Square onsite, the "Huffman tree" that isn't actually Huffman
Square onsite, the "Huffman tree" that isn't actually Huffman
Got rejected from Square after the onsite, email came 3 days later. Two coding, hotel booking SD, project deep dive. Pretty sure this coding round is what killed it.
Prompt: given a string of letters, count each char and build a binary tree from the counts. Leaves are the chars with their counts, internal nodes are just '#' with the sum of everything under them. Sounds like Huffman right? Nope. You sort chars by count ascending, ties broken by the char itself, glue the two smallest together (smaller on the left), and then every next char goes on the left with the whole tree so far on the right. So you end up with a lopsided chain.
I burned like 10 min writing a heap because I heard "frequency tree" and my brain went straight there. Then the tie break got me. Uppercase sorts before lowercase, so 'Z' comes before 'a', and I'd been lowercasing stuff in my head while tracing an example. Interviewer just watched me do it lol.
Once it worked he asked how I'd turn it into an actual Huffman tree, and then a height balanced version. Talked through the heap for the first one, ran out of time on the second.
Whatever, on to the next one.