How to Lose an IT Job in 10 Minutes
By: Albino Tonnina
Tags: Programming, JavaScript, Web Development, Interview

I’m gonna try to solve that first whiteboard test the way I should have done that day and I’m going to try to report the process here.

The Test

['Tokyo', 'London', 'Rome', 'Donlon', 'Kyoto', 'Paris'] // YOUR ALGORITHM [

[ 'Tokyo', 'Kyoto' ],

[ 'London', 'Donlon' ],

[ 'Rome' ],

[ 'Paris' ]

]

That’s it.

If you rotate the letters of each city you may or may not match another city. In case you do, put them together in a array on their own.

At work you’re escaping labyrinths of thousands of lines of code on a daily basis, how difficult is to show these people how to solve this simple and even kinda funny problem? Hold my beer.

Yeah…

Let’s pretend it went slightly differently.

The perfect whiteboard interview

Catchy paragraph title, although wrong. This is just the way I could have solved this test, both code and thought articulation. Who know how many other ways there are!

Me: thank you very much. I’ll gladly solve this problem for you (little bow)

Me: First question, do the letters just ‘rotate’ or they can be randomly mixed up?

Larry (to protect the privacy of the interviewers, I changed their name into a fancy one): Just rotate. First letter goes last, etc. One at a time.

IMPORTANT NOTE

There are a few comments of people proposing their solution to this problem. First of all thank you, one day I’m gonna look at them all and do something with them :) But it’s not the point of the article to get the most performant solution out of this. Also please pay attention to my last question to the interviewer. Many skip this part when they comment with their solution: letters rotate one by one. It’s not enough to .split and .sort the letters. If you do so, combinations like tokyo and yookt would match: they shouldn’t. Tokyo matches only with: okyoT, kyoTo, yoTok, oToky. See? 5 letters, 5 possible combinations. There’s an hint for you there. Let’s continue.

Me: right.

Larry: right.

Me: Right! I guess to start I would need a way to rotate the letters of each city. Tokyo would became okyoT, then kyoTo, oh cool! we’ve got kyoto now! Ok, I’m going to need a function to do that.

Me: I also need a way to loop through the input cities, ‘rotate’ the letters, do some matching and lastly grouping them together. I see that the input and the output are both arrays and they both contains the same values, the only difference is the depth of the two arrays, a flat one and an array of arrays.

Still me: I’m gonna write some pseudocode now to validate my thoughts. Later I’m going to test it too, cause I’m that good. 😏

function groupCitiesByRotatedNames(cities) { // use a variable to contain the output.

let output = []; // loop through each of the cities

// rotate the name in any possible combination

// check if the output contains a city that matches a combination

// add that city to the array containing the match

// otherwise add the city to the output as a new array return output;

}

Me, confident as a rockstar: let’s pseudotest my pseudocode (😛).

const input = ["Tokyo", "London", "Rome", "Donlon", "Kyoto", "Paris"]; console.log(groupCitiesByRotatedNames(input)); // That's how it would build up. The final output would be the last array: // [

// ["Tokyo"]

// ] // [

// ['Tokyo'],

// ['London']

// ] // [

// ['Tokyo'],

// ['London'],

// ['Rome']

// ] // [

// ['Tokyo'],

// ['London', 'Donlon'],

// ['Rome']

// ] // [

// ['Tokyo','Kyoto'],

// ['London', 'Donlon'],

// ['Rome']

// ] // [

// ['Tokyo','Kyoto'],

// ['London', 'Donlon'],

// ['Rome'],

// ['Paris']

// ]

Let’s start implementing this algorithm.

First, the utility to rotate the letters of the city names. I will create a utility function: