As I mentioned in the previous part, my phone interview was scheduled on 28th August 7:00 - 7:45 PM IST. The interview was over BlueJeans and the collaborative coding environment was coderpad.io. I already knew the name of my interviewer beforehand and did take a look at his LinkedIn profile and areas of interest.
So, instance #2 of being lucky - my interviewer was someone who regularly used OCaml and was in general a FP person (I knew this from his LinkedIn). Now, I am pretty well-versed in FP (not an expert or anything but I can write code which works), haskell and scala. I had also done a course on Functional Programming in the previous semester and so was in touch with puzzle questions which are typically solved using functional languages - list/tree traversals. I was also familiar with notations. I’m mentioning all the above because the knowledge actually helped in the interview.
So the question which was given to me was this (NOTE: This is nearly exactly what my interviewer wrote):
-- Given a tree which is defined as follows: data Tree a = Leaf a | Branch [Tree a] -- Write a function which takes a tree as input and returns a list -- of all leaf values func :: Tree a -> [a]
Essentially all I had to do was a simple in-order traversal while collecting values of the leaves but there were some subtleties. This definition of a tree, which has either:
- a leaf node with some value or
- branch node pointing to multiple other leaf/branch nodes,
is more popular in functional languages as its very
easy to pattern match over them while writing algorithms. Usually (read: in the Data
Structures course taught to us), all the nodes have values and I believe someone
not familiar with this would have been slightly confused by this form.
P.S. I had actually solved this exact question in one of the pattern matching assignments of the course mentioned before.
Looking back, I could have actually written a pretty elegant solution to this in haskell using pattern matching without too much effort (2 lines):
func (Leaf x) = [x] func (Branch xs) = concat $ map func xs
But somehow, I had gone into the interview thinking that I’ll code my solution in
C++ (which is sort of the standard for algorithmic questions) and I hadn’t written
haskell in like 3-4 months. I ended up writing a longer solution in C++ involving
a struct which stored a
boolean marking whether it was a leaf node or not and
two fields -
vector<Node *> other_pointers. Then it was a simple
in-order traversal implementation. Alongwith the null pointer error handling and
the manual list concatenation, I guess I wrote around 40 lines of code, which did
take some time.
Once my implementation was done, we discussed edge cases, cases when the function could fail and even solutions to it. And that was it. My interview lasted about 35-40 minutes.
I thought that usually 45 minute interviews have atleast 2 questions, so somehow I had performed badly - using up all the time on only one question, but my interviewer had mentioned somewhere in between that this was the only question he had brought so I wasn’t too scared.
At the same time slot and date when I had my interview, another one of my friends also had his
phone interview. We both had been referred by Sayash and had the exact same date and time slots.
His interviewer asked him more standard algorithmic questions like evaluating an expression having
-s and another question I don’t remember (but I remember that it would
have been difficult for me to solve). So, lucky again!!
My interviewer had mentioned that they would get back to me within 2 weeks, so I was ready for the wait. To my surprise and happiness, I received a mail at 9:00 a.m. the very next day (29th August) informing me that they’ll be moving forward with the next stage of the interview which were the onsite rounds in the London Office.
Details of the onsite rounds in the next part.