← Questions
Most frequent key without max()
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Apple's technical phone screen, last asked Nov 2025. It tests hash map and python.
A small Python object-oriented exercise. A Parent class takes a list of items and, in its constructor, builds self.ldf: a dictionary from each item's string form (str(item)) to how many times that string occurs. Counting is case-sensitive. Write the Child subclass method that returns the most frequent keys, without calling the built-in max().
Interface
Unless you agree on a different interface with the interviewer, assume:
-
class Child(Parent)withmost_frequent_key(self) -> list[str]: every key with the highest count, in the order the keys first appear in the input. An empty input gives[]. - Use the precomputed
self.ldf; don't recount.
Example
Child(["tea", "Tea", "coffee", "tea", "Tea", "juice"]).most_frequent_key()
-> ["tea", "Tea"] # 2 each; case matters
Child(["7", 7, "seven", "Seven", "seven", 7.0]).most_frequent_key()
-> ["7", "seven"] # "7" and 7 share the key "7"; 7.0 becomes "7.0"
Child([]).most_frequent_key() -> []
Constraints
- Up to
10^5items; each can be turned into a string withstr(). - No
max(), and nosorted()orCounter.most_common()to get around it.
Clarifying questions
- On a tie, return all tied keys, or one?
- In what order should tied keys come back?
- Should the method work if
ldfis empty?
What a strong answer covers
- One pass over
self.ldf.items()tracking the best count and a list of keys: reset the list on a higher count, append on an equal one. O(k) for k distinct keys. - Python dictionaries keep insertion order, so first-appearance order comes for free.
- How inheritance gives
Childaccess toself.ldfwithout calling anything extra. - Tests: all distinct, all equal, mixed types, empty.
Follow-ups
- Return the top
nkeys without sorting all of them (a heap). - Make counting case-insensitive without changing
Parent. - The items arrive as a stream too large to keep in memory.
Where it's asked
- Apple · Technical phone screen · Last asked Nov 2025
What it tests
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
