Fivetran | Data Engineer phone interview | Decode compressed string to frequency array
Anonymous User
660

The coding assessment was hosted on HackerRank. I am providing a curated, easy-to-understand version of the question. The actual question, with explanations and examples, was unnecessarily wordy. It was more than a page long.

It was an advanced version of run-length encoding. I couldn't find the question on Leetcode. Maybe someone can refer me to the actual question if it's available somewhere or provide a link to a similar one.

I wasn't able to solve it in the interview. So, your solutions in the comments are most welcome!

Question
The input string contains digits 0-9 and parentheses (). The digits outside the parentheses are mapped to lowercase English alphabets. The digits inside the parentheses are mapped to the frequencies of the corresponding alphabets in the order that they appear in the string .

Example 1
Input string: '1224#26#'
This input string is an encoding of 'abxz'.

  • 'a' to 'i' is encoded from '0' to '9' respectively. So, '1' maps to 'a' and '2' maps to 'b'.
  • 'j' is encoded as '10#', 'k' as '11#', and so on, with the last alphabet 'z' as '26#'.

Example 2
Input string: '1(2)2(3)324#26#(5)'
This input string is an encoding of 'aabbbcxzzzzz'

The encoding is similar to the example above except the parentheses contain the frequency of each corresponding character when it's greater than 1. For example,

('->' stands for 'maps to')
In 'aa', a is seen 2 times, so a -> 1 and 2 times -> (2); therefore 'aa' becomes 1(2).
In 'zzzzz', z is seen 5 times, so z -> 26# and 5 times -> (5); therefore 'zzzzz' becomes 26#(5).

Return

Given a string, return a frequency array containing the counts of the alphabets in the string.

For example, the input string 'abxz' should return:

[1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1] -> You need to return this array.
[a.b................................................................x....z] -> I drew this for your convenience.

My performance
My solution was able to crack the first example but not the second one because I forgot to account for the parentheses when I started to code. My bad. Later, I tried to fix my code but I didn't have enough time. When I was walking the interviewer through my thought process, I explained that I need to account for it but forgot about it later for some reason. I guess it was interview pressure.

The interviewer did not stop me from making the mistake before I got too deep into the trenches. A lesson learned the hard way! After going through many discussions forums, interview preparation guides, and coaching websites, I was under the assumption that interviewers usually stop the candidate from making an obvious mistake by providing subtle hints but it's not true all the time. My bad again for making such as assumption.

What would be the difficulty of this question? Easy, Medium, Hard..?

Comments (3)