27 comments

[ 4.5 ms ] story [ 69.9 ms ] thread
This is trivial for me to solve, sitting here at my computer. Would I be a me to solve it on a whiteboard in front of a group of interviewers? Maybe not. The stress and anxiety may get the best of me.
I agree with you; it is easy solving sitting at the computer, but in front of a group of interviewers might be stressful like that.

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.

Clickbait title
Given that those exact words are the first line of the tweet and that it is almost the entire point of the post, what alternative title would have been better?
Something like "Most candidates that I interviewed cannot write a function that does run-length encoding"
Can I solve it? Yes. Is it a reflection of how well I can do my job? No. Will I be solving these kinds of problems at this job? Very unlikely.

This is a massive waste of everyone's time and tells you nothing about the candidate's ability to do the job. Try again.

If most candidates cannot solve it in 25 minutes (while being a simple task), then it is a valid test

    [(c, sum(1 for _ in same)) for c, same in itertools.groupby(s)]
Does this a actually solve it? It should only sum when the character next to is the same as the previous. a is counted twice: ([a, 3], ... [a, 1])

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

Yes, it does solve it. It produces the expected output (`a` should occur twice)

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
Ok, thanks. I don‘t speak enough python to understand that it solves the issue.
Please do comment if you downvote a correct solution.
This is seemingly the software equivalent of those trivial order of operations problems you see reposted over and over again on Facebook/Twitter.

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.

I think this approach might backfire: If purpose of the question is to test the candidate's knowledge of common libraries, then it probably is way to easy to be indicative, this kind of knowledge should be tested for something the candidate can't find by googling for 5 minutes. If the purpose is to test algorithm design, its also way to basic.
While most of the comments on the Twitter thread use fancy functions from common libraries, you should be able to solve this problem without the need for googling and without those fancy functions.

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.

Notably, none of the answers given (despite being clever solutions to get the data) output the exact format requested. [("..",..),...]
If I gave a correct answers to a question like this and they responded as you suggest, I’d leave immediately. The purpose of this line of questioning is to weed out incompetent programmers, not be petty. If the latter is a priority, then working with them will undoubtedly be a trial.
possibly because it's much harder (maybe impossible?) to do in languages that don't have native tuples, which appear to be the ask.

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.
> 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.

Hello, Hacker News! I'm new to the community. I just saw this and thought it'd be fun to solve in Haskell. The first solution I thought of was done in under a couple minutes. The second solution (I wanted to implement `group` myself) took a lot longer because I messed up a pattern match!

https://gist.github.com/hyperrealgopher/19730725804ba8825d50...

> module Main where

> 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.

(comment deleted)
Great solution that avoids using any functions that do the heavy lifting for you! Thanks for showing!
My own first idea was actually similar to yours, although I wrote:

  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.)
(comment deleted)
If nothing else this makes me feel better about my own abilities. Unfortunately I find it's not my ability to solve tricky puzzles but my ability to solve business problems in minimal time. Often the hard part is not the algorithm design but extracting the requirements that will bring the most benefit to the business.