Twillio | OA | Prison Break
Anonymous User
15775

I have more than 6 years of experience, I recently got twillio OA.
It was for 75 minutes, there were two questions.

  1. Prison Break, I found the question explained here
  2. you are given a list of numbers(could repeat), sort them initially by their frequency and then by their value. same as this

For ex:
INPUT: 3,4,2,3
OUTPUT: 3,3,2,4

I solved first one completely, second one had few failing cases I think because of timeouts because I solved them using Java 8 lambdas and multiple sorts and generating streams which caused it to be slow and I didn't have enough time to rewrite it in the normal way.

============EDIT==============
My solution was something like,

Create a frequency map with key as the number and its frequency as the value, lets call that 'fr'
return fr.entrySet().stream().sorted(MyComaparator).flatMap(s -> Stream.generate(s.getKey()).limit(s.getValue())).collect(Collectors.toList())

class MyComparator will have normal comparison logic, first compare frequency and then compare value.

Thanks to @mukesh_kumar for suggesting the leetcode problem for the second one. I have added that link as well

Comments (7)