Katkat

Implement most frequent key without using max()

AppleAnalytics Engineer · Technical Screen

Last updated

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.

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
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:

queries = ['park', 'Park', 'McDonalds', 'apple', 'Apple Park', 'park', 'Park']
x = Child(queries)
print("most frequent:", x.most_frequent_key())

Expected output:

most frequent: ['park', 'Park']

Implement Child.most_frequent_key().