Implement most frequent key without using max()
Company: Apple
Role: Analytics Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
You are given the following Python class skeletons. The `Parent` class precomputes:
- `self.l`: the original list
- `self.lf`: a `set` of the stringified items in the list
- `self.ldf`: a `dict[str, int]` mapping each stringified item to its frequency
```python
class Parent:
def __init__(self, l):
self.l = l
self.lf = set(str(s) for s in self.l)
self.ldf = {}
for item in self.l:
s = str(item)
if s not in self.ldf:
self.ldf[s] = 1
else:
self.ldf[s] += 1
class Child(Parent):
def most_frequent_key(self):
"""Return the key(s) with the highest frequency.
Constraints:
- You may NOT use Python's built-in `max()`.
- Do not use `collections.Counter`.
- Find the highest count manually (via iteration).
Output:
- Return a `list[str]` of all keys that are tied for the highest frequency.
- If multiple keys tie, return them in the order of first appearance in the original input list.
"""
# TODO: implement
return output_list
```
Example:
```python
queries = ['park', 'Park', 'McDonalds', 'apple', 'Apple Park', 'park', 'Park']
x = Child(queries)
print("most frequent:", x.most_frequent_key())
```
Expected output:
```text
most frequent: ['park', 'Park']
```
Implement `Child.most_frequent_key()`.
Overview: Category: Coding & Algorithms — this problem evaluates frequency counting, dictionary-based aggregation, string handling, manual maximum-finding without built-in functions, and stable tie-breaking by first appearance in the input.
Read the full Apple Analytics Engineer interview experience this question came from
Return all stringified keys tied for highest frequency, preserving first appearance order, without using max or Counter.
Examples
Input: (['park', 'Park', 'McDonalds', 'apple', 'Apple Park', 'park', 'Park'],)
Expected Output: ['park', 'Park']
Explanation: Prompt example.
Input: ([],)
Expected Output: []
Explanation: Empty list.
Hints
- Count manually, scan once for the best count, then scan original key order for ties.