Hacker News new | ask | show | jobs
by filoleg 903 days ago
> "wasn't sure how to look through the grid in a circle pattern"

Now I am curious to know whether I am too dense to get it or if the candidate was just that off the mark.

Is circle pattern sorta like iterating through a 2d array like a spiral (i.e., outer layer of the 2d-shape first, then one deeper, etc.)? And if yes, why would that ever be useful for just searching for a specific value in a 2d array?

I get how it could be useful for some more niche/specific problems where the layering of the 2d array would actually matter, but is it just entirely off the chain to recommend it here? Because I cannot for the life of me figure out why you would want to do that instead of just iterating, especially considering how significantly less trivial it is to code-up that “circular” iteration (as opposed to just a regular linear iteration).

Sidenote: Is there even a more efficient way to solve that problem, other than just sequentially iterating through the 2d array and simply tracking the value/position of the largest number until you finish iterating over the entire 2d array (assuming it is non-sorted)? It seems way too simple, so I feel like either I am missing something about the problem statement or there is a better solution than the one I proposed.

3 comments

This was part 1 of a 2 or 3 part question. First we literally iterate through each element, keeping track of the max. I use this question for interns, too, so it's intentionally super easy. It gets harder in part 2.

For clarity, the pseudo code solution to the question is

    for row in a[0]:
        for e in row:
            max_so_far = max(e, max_so_far)
    return max_so_far

No tricks. Just an initial weeder question for interns before we move onto the real question.
Ah, makes perfect sense, thank you for clarifying. Your pseudo code solution confirms that I understood the problem statement correctly.

Out of pure curiosity, what were the follow up (part 2 and 3) questions? Not looking for a solution, but if you could post the problem statement, it would be very appreciated. If you feel uncomfortable sharing it publicly out of some concern, that’s entirely fair, no worries.

I think the point of the OP was A the person wanted to solve this in a spiral pattern, which isn't necessary. And B didn't know how to do a spiral pattern, which is fairly simple. As to your other question, unless it is directed you need to look at each element. The real question is how to iterate over a 2d array. There are several methods and really that is all this question is looking for.
Most day-to-day coding is simple and boring. Your interview questions should be, too. I've interviewed 100's of candidates over the years. Many of them had trouble writing a couple of for loops. This was for a somewhat similar problem. I would stress "we don't need an optimal solution, we just need a working solution."