Re:Microsoft changed how it interviews software developers
by m0zg on Sunday December 30, 2018 @06:01PM
(#18791858)
It's neither. There's a DP solution for this problem and a recursive solution without memoization (with exponential complexity). The shortest solution I have seen was 18 lines of code.
[ Parent
| on HN ]
Re:Microsoft changed how it interviews software developers
by epicureanideal on Sunday December 30, 2018 @07:30PM
(#18792192)
Please show me a regex parser in 18 lines of code! Even 36! I did this exercise once and it took 100s of lines, I think using C++... but it's been a long time. Maybe it's significantly shorter in Python? But still 18 lines is impressive.
Re:Microsoft changed how it interviews software developers
by psurge on Sunday December 30, 2018 @08:06PM
(#18792353)
I probably screwed something up, but here's a 16 line attempt in Python, where the regex specials are limited to .?:
def matches(pattern, s):
if len(pattern) == 0:
return True
c = pattern[0]
assert c not in ("*", "?")
if len(pattern) > 1:
if pattern[1] == "?":
return (
len(s) > 0 and c in (".", s[0]) and matches(pattern[2:], s[1:])
) or matches(pattern[2:], s)
elif pattern[1] == "*":
for i, d in enumerate(s):
if c not in (".", d):
break
return any(matches(pattern[2:], s[j:]) for j in range(i, -1, -1))
return len(s) > 0 and c in (".", s[0])
Re:Microsoft changed how it interviews software developers
by m0zg on Sunday December 30, 2018 @08:26PM
(#18792446)
Pretty much something along these lines, except it's also required that both pattern and s end at the same time for a full match (this complicates the base case a tiny bit). The C/C++ solution looks more elegant because you can use pointers. Note that this particular version requires no prior knowledge of the algorithm and can be solved on the spot fairly easily.
Re:Microsoft changed how it interviews software developers
by epicureanideal on Monday December 31, 2018 @12:13AM
(#18793137)
Thanks for the demonstration!