Watch 2 video solutions for Find the Most Common Response, a medium level problem involving Array, Hash Table, String. This walkthrough by Programming Live with Larry has 188 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given a 2D string array responses where each responses[i] is an array of strings representing survey responses from the ith day.
Return the most common response across all days after removing duplicate responses within each responses[i]. If there is a tie, return the lexicographically smallest response.
Example 1:
Input: responses = [["good","ok","good","ok"],["ok","bad","good","ok","ok"],["good"],["bad"]]
Output: "good"
Explanation:
responses = [["good", "ok"], ["ok", "bad", "good"], ["good"], ["bad"]]."good" appears 3 times, "ok" appears 2 times, and "bad" appears 2 times."good" because it has the highest frequency.Example 2:
Input: responses = [["good","ok","good"],["ok","bad"],["bad","notsure"],["great","good"]]
Output: "bad"
Explanation:
responses = [["good", "ok"], ["ok", "bad"], ["bad", "notsure"], ["great", "good"]]."bad", "good", and "ok" each occur 2 times."bad" because it is the lexicographically smallest amongst the words with the highest frequency.
Constraints:
1 <= responses.length <= 10001 <= responses[i].length <= 10001 <= responses[i][j].length <= 10responses[i][j] consists of only lowercase English lettersProblem Overview: You receive a collection of responses (strings) and must determine which response appears most frequently. The task is essentially a frequency counting problem where you track occurrences and return the response with the highest count.
Approach 1: Brute Force Frequency Counting (O(n²) time, O(1) space)
The most direct strategy compares every response with every other response and counts how many times each appears. For each index i, iterate through the entire list and count matching values. Track the maximum frequency encountered while scanning. This approach works without additional data structures but performs redundant comparisons, making it inefficient for large inputs. Time complexity is O(n²) and space complexity is O(1). It demonstrates the basic idea of counting occurrences but rarely passes strict performance constraints.
Approach 2: Sorting + Linear Scan (O(n log n) time, O(1) or O(n) space)
Sorting groups identical responses together so frequency counting becomes a single pass. First sort the array of responses. Then iterate through the sorted list and count consecutive duplicates, updating the most frequent response whenever a larger streak appears. Sorting costs O(n log n) time, while the scan is O(n). Extra space depends on the language’s sorting implementation (often O(1) to O(n)). This method is useful when modifying the array is allowed and you want a simple implementation without additional lookup structures.
Approach 3: Hash Table Counting (O(n) time, O(n) space)
The optimal solution uses a hash table to store frequency counts while iterating once through the responses. For each string, increment its counter in the map. During the same pass (or a follow-up scan of the map), track the response with the highest frequency. Hash table lookups and updates run in constant average time, producing overall O(n) time complexity with O(n) additional space for storing counts. This approach directly leverages common patterns from array traversal and counting problems, making it both efficient and straightforward to implement.
Recommended for interviews: Interviewers expect the hash table counting approach. Starting with the brute force explanation shows you understand the problem mechanics, but transitioning to a frequency map demonstrates algorithmic optimization. The HashMap/dict solution achieves linear time and clean logic, which is typically the target complexity for problems involving frequency analysis of strings.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Counting | O(n²) | O(1) | Small datasets or when avoiding extra memory is critical |
| Sorting + Linear Scan | O(n log n) | O(1)–O(n) | When sorting is acceptable and you want a simple counting pass |
| Hash Table Counting | O(n) | O(n) | General case and expected interview solution for frequency problems |