An alternative approach to this question is to put the cities into an easier to work with data structure, such as a map (aka object) of sets, with the key being a canonical rotation, and then translate that into the desired array of arrays in the output. (Here is that as a gist. )
You didn't ask for suggestions on the solution, but I couldn't resist playing around with the exercise. I might have drawn a blank if asked to come up with this at a whiteboard though. My first solution was four nested loops.
I do whiteboard interviews (interviewers side ;) ).
A clean and readable solution with nested loops would have been perfect for me.
I laughed so hard at "four nested loops" 😂😂😂
Hi Dustin, thanks for your reply!
I didn't ask for suggestion because I'm sure there are so many solutions to this test!
More performant, more declarative, more functional, more more more :)
Someone may even find an O(1) version of this, I don't know :D
As you said the problem is doing it there, under pressure. I like to write elegant code, I like approaching problem methodically, I do like these problems. But my method it just doesn't work in a context like that. Not when the stakes were so high.
@poof – speak truth my man!
We're a place where coders share, stay up-to-date and grow their careers.
We strive for transparency and don't collect excess data.