Haskell, Microsoft, and interview questions

This post borders on being off-topic, but it seems like people on this list would be interested. First, some of you know that I had the distinct privilege of flying out to Microsoft this week to interview for a Software Development Engineer position on their Live Search team. So I want to first tell you about their reaction to my Haskell experience, and then give you some interesting coding problems that they asked me to solve, which could be good practice to tackle in Haskell. So the first task I was given to do, long before my in-person interview, was to complete a technical assessment. This consisted of two fairly simple algorithms to write. I chose to do so in Haskell. On the basis of this assessment, they decided I was worth a phone interview. When I got on that interview, one of the first things the interviewer said was "So, one thing that jumped out immediately was that you did the assessment in Haskell. Tell me a little bit about that." It led to a good discussion about why I like Haskell and functional programming. When I actually flew out there, several people made comments about me being "that guy who writes Haskell". The recruiter even said something along the lines of "anyone who knows haskell is certainly worth our time to talk to." Moral of the story: Haskell rocks, and even Microsoft knows it! That said, here are some interesting problems to work through in Haskell, if you'd like: 1.) A "rotated array" is an array of integers in ascending order, after which for every element i, it has been moved to element (i + n) mod sizeOfList. Write a function that takes a rotated array and, in less-than-linear time, returns n (the amount of rotation). 2.) You are given a list of Ball objects. Each Ball is either Red or Blue. Write a function that partitions these balls so that all of the balls of each color are contiguous. Return the index of the first ball of the second color (your result can be Red balls, then Blue balls, or the other way around). In haskell, you'll probably want to return a ([Ball],Int). 3.) Live Search is a search engine. Suppose it was to be tied into an online store. Now you're given two lists. One is a [(SessionId, NormalizedQuery)]. That is, when a particular user performs a query, it is turned into some consistent format, based on their apparent intent, and stored in this logfile. The second list is a [(SessionId, ProductId)]. This indicates the product bought by a particular user. Now, you want to take these two (potentially very long) lists, and return some structure that will make it easy to take a query and return a list of the most popular resulting purchases. That is, of people who have run this query in the past, what are the most common products they've wound up buying? The interviewer said that this is an instance of a well-known problem, but I didn't catch the name of it. 4.) You're given an array which contains the numbers from 1 to n, in random order, except there is one missing. Write a function to return the missing number. 5.) Write a function to reconstruct a binary tree from its preorder traversal and inorder traversal. Take into account that the traversals could be invalid. 6.) You have a [(WeatherStationId, Latitude, Longitude)]. Similar to #3, write a function which will, off-line, turn this into a data structure from which you can easily determine the nearest Weather Station, given an arbitrary Latitude and Longitude. 7.) Write a function for scoring a mastermind guess. That is, in a game of mastermind (http://en.wikipedia.org/wiki/Mastermind_(board_game)), given a guess, and the actual answer, determine the number correct, the number wrong, and the number out of place. 8.) Implement a trie (http://en.wikipedia.org/wiki/Trie) data structure. Write a function add, which takes a word, and a trie, and adds the word to the trie. Write another function lookup, which takes a prefix and a trie, and returns all the words in the trie that start with that prefix. 9.) Write an algorithm to shuffle a deck of cards. Now write a function to perform some kind of evaluation of "how shuffled" a deck of cards is. Hope you enjoy!

On Wed, 25 Jun 2008, Andrew Wagner wrote:
The recruiter even said something along the lines of "anyone who knows haskell is certainly worth our time to talk to." Moral of the story: Haskell rocks, and even Microsoft knows it!
Interesting. That is they are aware what they support in Cambridge for years?

I would guess that there are a lot of things their research department
does that doesn't get used or thought about on a daily basis by the
implementation teams. Either way, the sarcasm is not necessary.
On Wed, Jun 25, 2008 at 4:49 PM, Henning Thielemann
On Wed, 25 Jun 2008, Andrew Wagner wrote:
The recruiter even said something along the lines of "anyone who knows haskell is certainly worth our time to talk to." Moral of the story: Haskell rocks, and even Microsoft knows it!
Interesting. That is they are aware what they support in Cambridge for years?

On 2008 Jun 25, at 16:49, Henning Thielemann wrote:
On Wed, 25 Jun 2008, Andrew Wagner wrote:
The recruiter even said something along the lines of "anyone who knows haskell is certainly worth our time to talk to." Moral of the story: Haskell rocks, and even Microsoft knows it!
Interesting. That is they are aware what they support in Cambridge for years?
The sales office doesn't always pay attention to MSR. -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH

On 26 Jun 2008, at 8:14 am, Andrew Wagner wrote:
6.) You have a [(WeatherStationId, Latitude, Longitude)]. Similar to #3, write a function which will, off-line, turn this into a data structure from which you can easily determine the nearest Weather Station, given an arbitrary Latitude and Longitude.
I actually set something not entirely unlike this as a problem in a 4th year OO class. Despite telling them over and over and over again that the Earth is not flat, and despite telling them how to convert (Latitude, Longitude) to 3-dimensional coordinates on the unit sphere, and despite telling them that greatest circle distance between two points on the surface of the Earth is a monotone function of Euclidean distance between points on the unit sphere, so that all they had to do was to glue these pieces together, EVERY SINGLE ONE used Euclidean distance between (Latitude, Longitude) pairs as if they were co-ordinates on a flat 2-d plane. And we even live near the International Date Line, which is where this mistake goes horribly wrong. Did they score you on coding or on geometry? For what it's worth, a 3-dimensional kd tree really flew on this problem.

Did they score you on coding or on geometry?
It was definitely more on coding and my ability to think about the problem.
For what it's worth, a 3-dimensional kd tree really flew on this problem.
I did some reading up on this, and it seems interesting. It would be need to implement something like this in Haskell, but I can't seem to find any detailed specs on the data-structure. Got any recommendations?

On 6/26/08, Andrew Wagner
Did they score you on coding or on geometry?
It was definitely more on coding and my ability to think about the problem.
For what it's worth, a 3-dimensional kd tree really flew on this problem.
I did some reading up on this, and it seems interesting. It would be need to implement something like this in Haskell, but I can't seem to find any detailed specs on the data-structure. Got any recommendations?
http://en.wikipedia.org/wiki/Kd_tree -- Sebastian Sylvan +44(0)7857-300802 UIN: 44640862

On 27 Jun 2008, at 3:11 am, Andrew Wagner wrote: For what it's worth, a 3-dimensional kd tree really flew on this problem.
I did some reading up on this, and it seems interesting. It would be need to implement something like this in Haskell, but I can't seem to find any detailed specs on the data-structure. Got any recommendations?
http://en.wikipedia.org/wiki/K-d_tree is perhaps the obvious place to start. There's a link there to Jon L. Bentley's original (1975) paper, which is really very clear. The key to getting an efficient version in C was to specialise the code to the particular k that I wanted (k=3). Of course there are much newer data structures that can do all sorts of things, but for simply finding the closest point in 1, 2, 3, or 4-dimensional space k-d trees are darned good. There isn't much that works well for high dimensions. Last year I marked a 4th year project that studied/evaluated a comparatively recent data structure that was said to be good for high dimensions. I had difficulty understanding it, because I couldn't see where the recursive step was. The answer was that there wasn't one: the algorithm worked better for high dimensions than other trees because it turned into a simple linear search after one partitioning step. In effect, it was too dumb to keep tripping over its own feet. So _really_ don't expect k-d trees to work well for large k.

On Thu, Jun 26, 2008 at 8:11 AM, Andrew Wagner
I did some reading up on this, and it seems interesting. It would be need to implement something like this in Haskell, but I can't seem to find any detailed specs on the data-structure. Got any recommendations?
Specialised for 2d only, but: http://www.imperialviolet.org/binary/NearestNeighbour2D.hs AGL -- Adam Langley agl@imperialviolet.org http://www.imperialviolet.org

On 27 Jun 2008, at 11:36 am, Adam Langley wrote:
Specialised for 2d only, but:
In my C code for this, specialised to 3D, - dimension numbers were never stored - no arrays were used - the "search in x" function called the "search in y" function, which called the "search in z" function, which called the "search in x" function Here's the relevant part of my kd3.h: typedef struct KD3_Node *kd3ptr; struct KD3_Node { double x, y, z; kd3ptr left, right; bool present; << payload data goes here >> };
participants (6)
-
Adam Langley
-
Andrew Wagner
-
Brandon S. Allbery KF8NH
-
Henning Thielemann
-
Richard A. O'Keefe
-
Sebastian Sylvan