Java Data Structures limits for coding interviews?

I've been asked a couple of problems in FAANG phone interviews recently where I felt using a language different from Java would have been way easier.

Example:
Given a list of integers, print the most K frequent elements.
https://leetcode.com/problems/top-k-frequent-elements/

During the interview, I went through the different solutions that came to my mind:

Approach one: have an element -> frequency map. Once you scan the whole input list, and have the frequencies for all elements, you sort the entryList by values (frequency) descendingly, then pick the first K items. Complexity is O(n*log(n)).
Super easy!

Approach Two: this is where Java can be more difficult than other languages:
Maintain 2 data structures: 1st heap of each elemnt frequency seen so far. 2nd a map element -> internal heap node holding the frequency for that element. This 2nd structure would allow us to heapifyElementUp directly using the element pointer.
We use heapifyElementUp to update the frequency node location in the heap, when the frequency is increased.

While this approach would still have the worst case complexity of O(n*log(n)), it should do better on average because it minimizes the heap moves.

Languages like C++ (and I think Python) allow you to have access to internal data structures. So for a C++ heap, you'd be able to keep a pointer to the internal heap node.
This would have made my solution very simple!
While in Java, I had to implement my own Heap, HeapNode data structures to be able to proceed with this approach. I didn't finish because, obviously, heap has many methods to impelement.

Finally to my question!

Do you think, Java is a worse fit for such kind of problems?
OR do you think that I should have simply went with Approach 1, or maybe a variation of approach 2 where I use Java's heap PriorityQueue only after I have the frequency for all elements, and simply insert all frequenies and top the first K elements?

All feedback is appreciated!

Comments (1)