Crossover for Work Interview Question

They asked me to solve a simple coding challenge (probably from the book CTCI)