Most Frequent String in an Array
Problem Given an array of strings, find the string that occurs most frequently.
Input / Output
- Input: array
wordsof strings. - Output: the most frequently occurring string. Clarify tie-breaking with the interviewer — first occurrence, lexicographically smallest, or any.
Constraints
- Up to ~10^5 strings, duplicates expected.
- Strings may be of arbitrary length
k, which matters: hashing a string is O(k), not O(1). - The array may be empty — decide the contract (null, exception, or sentinel).
Example
- Input:
["hello", "world", "world", "bar", "hello", "hello"]→ Output:"hello"(frequency 3). - Tricky case:
["a", "b"]— an all-distinct array where every count is 1, so the tie-break rule decides the answer. This is the clarifying question worth asking up front.
asked …