Most candidates cannot solve this interview problem
twitter.com
twitter.com
The method to solve it depends on the programming language. In this case, I would use Haskell; however, this would likely be a part of a larger program, and in that case the programming language that the larger program is written in would be used instead.
The input string is quoted, so the output appears to be an array (not a string type).
My stab at it in Javascript resulted in:
function collapse(a) {
collapsedArray = [];
for (let i=0; i<a.length; i++) {
let count = 1;
let currentchar = a[i];
while (i+1 < a.length && a[i+1] == currentchar) {
i++;
count++;
}
collapsedArray.push([a[i], count]);
}
return (collapsedArray);
}
console.log(JSON.stringify(collapse("aabbcdde")));
This outputs `[["a",2],["b",2],["c",1],["d",2],["e",1]]`. Without native tuples in Javascript (or JSON), I could construct a string which replaces the inner brackets with parens, but that would be pretty contrived, as the output requested isn't quoted.Input: "aaaabbbcca"
Output: [("a", 4), ("b", 3), ("c", 2), ("a", 1)]
The input and output is explicit. The interpretation is a talking point.
Serialization into parens is not native for most? scripting languages. Creating a string would probably be most efficient for the output format, for sure. It's important to remember that interview questions are not just about the algorithm (for which this is trivial) but the requirements. Asking if the double quotes are representing strings (and what kind of strings for some languages) is part of the natural back-and-forth of an interview.
If so, this can, too.
While I agree that this is way to basic to test AD skills, if it's really true that there is a sizable percentage of people that is unable to solve this problem, this might be a good way to weed those out, before starting with the 'real' questions.
https://gist.github.com/hyperrealgopher/19730725804ba8825d50...
> input = "aaaabbbcca"
> r :: String -> (Char,Int) -> [(String, Int)]
> r [] (c,n) = [([c],n)]
> r (x:xs) (c,n)
> | c == x = r xs (c,n+1)
> | otherwise = (([c],n): r xs (x,1))
> main = putStrLn (show (r (tail input) (head input, 1) ))
4 minutes 42 seconds.
And no, I don't write code like this normally, but I guess this is me when I'm under time pressure... anyway, I could refactor it nicely afterwards in the 25 minutes allocated.
map (head &&& length) . group
(Also, it took me only a few seconds, because it just seemed obvious to me, even though Haskell is not the programming language I normally use for my projects, but it seems the right thing for this.)Thanks for sharing!
I can never tell if the objective is to give novel solutions to the problem or to just publicly show that you can solve it. In any case, I always chuckle when I see how some of those in the comments can reply so smugly to others.
This is a massive waste of everyone's time and tells you nothing about the candidate's ability to do the job. Try again.
[(c, sum(1 for _ in same)) for c, same in itertools.groupby(s)]While I agree that such interview questions are bad, they also give the interviewer a view on how the interviewee attempts to solve a problem.
Edit: typo
Here's solution that doesn't use itertools.groupby() https://docs.python.org/3/library/itertools.html?highlight=i... :
result = []
prev, count = None, 0
for c in s+"\0": # note: assuming "\0" can't be in `s`
if c != prev: # starting new sequence
if count: # save count for old sequence
result.append((prev, count))
prev, count = c, 1 # reset count
else: # continuation of old sequence
count += 1