LeetCode Problem

How to Solve Find the Most Common Response

The task asks to find the most common response across multiple survey days, ensuring duplicates within each day are removed. If there is a tie in frequency, return the lexicographically smallest response. Hash tables and array scanning are key to solving this efficiently.

GhostInterview Help

Need help with Find the Most Common Response without spending extra time grinding it?

GhostInterview can read Find the Most Common Response from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #3527Array scanning plus hash lookupReviewed 2026-03-08
Difficulty
Medium
Primary pattern
Array scanning plus hash lookup
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

The task asks to find the most common response across multiple survey days, ensuring duplicates within each day are removed. If there is a tie in frequency, return the lexicographically smallest response. Hash tables and array scanning are key to solving this efficiently.

Problem Statement

You are given a 2D array responses, where each responses[i] represents survey responses for the ith day. The task is to determine the most frequent unique response across all days after removing duplicates within each individual day's response array.

If there is a tie for the most frequent response, return the lexicographically smallest one. Your solution should be efficient enough to handle up to 1,000 days and 1,000 responses per day, making array scanning and hash lookup important to solving the problem.

Examples

Example 1

Input: responses = [["good","ok","good","ok"],["ok","bad","good","ok","ok"],["good"],["bad"]]

Output: "good"

Example 2

Input: responses = [["good","ok","good"],["ok","bad"],["bad","notsure"],["great","good"]]

Output: "bad"

Constraints

  • 1 <= responses.length <= 1000
  • 1 <= responses[i].length <= 1000
  • 1 <= responses[i][j].length <= 10
  • responses[i][j] consists of only lowercase English letters

Solution Approach

Step 1: Remove Duplicates Within Each Day

The first step is to ensure that there are no duplicates within each day's response array. This can be achieved by converting each day's list of responses into a set, which automatically removes duplicates.

Step 2: Count Frequency of Each Response

After eliminating duplicates within each day, you can count the frequency of each unique response using a hash table (or HashMap). This will help efficiently track the occurrence of each response across all the days.

Step 3: Find the Most Common Response

After counting the frequencies, you need to find the response with the highest count. In case of a tie (same frequency), the lexicographically smaller response should be returned. This can be done using simple comparison logic while iterating through the frequency map.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

The time complexity is determined by the need to process each response within each day, resulting in O(N * M) where N is the number of days and M is the average number of responses per day. The space complexity is O(N * M) to store the responses and their counts in the hash map.

What Interviewers Usually Probe

  • Can the candidate efficiently handle large input sizes?
  • Does the candidate apply hash maps effectively for counting frequencies?
  • How well does the candidate handle tie-breaking between lexicographically equal counts?

Common Pitfalls or Variants

Common pitfalls

  • Not removing duplicates within each day before counting frequencies can lead to incorrect results.
  • Failure to account for ties in frequency, which may lead to returning the wrong response.
  • Inefficient handling of large input sizes, especially with array scanning or hash lookup.

Follow-up variants

  • Handle responses with varying case sensitivity (e.g., 'good' vs 'Good').
  • Find the second most common response instead of the first.
  • Return the least common response instead of the most common.

How GhostInterview Helps

  • GhostInterview helps break down the problem into clear steps, focusing on array scanning and hash map usage for efficiency.
  • It assists in identifying key patterns for optimizing time and space complexity during interviews.
  • GhostInterview suggests common pitfalls and helps candidates avoid issues with large input handling and lexicographical ties.

Topic Pages

FAQ

What is the main pattern used in 'Find the Most Common Response'?

The primary pattern is array scanning combined with hash map lookup to efficiently count occurrences and handle ties.

How do I handle ties in the frequency of responses?

If multiple responses have the same frequency, return the lexicographically smallest one.

What is the time complexity of this problem?

The time complexity is O(N * M), where N is the number of days and M is the number of responses per day.

Why do I need to remove duplicates within each day?

Duplicates within each day would distort the frequency count and lead to incorrect results. Removing them ensures accuracy.

Can I solve this problem without using hash maps?

While it's possible, hash maps provide an efficient way to count frequencies and are optimal for this task, especially with large inputs.

GhostInterview Solver

Need direct help with Find the Most Common Response instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Find the Most Common Response from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.