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 <= 1000
1 <= responses[i].length <= 1000
1 <= responses[i][j].length <= 10
responses[i][j]
consists of only lowercase English lettersWhen you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to finding the most common response involves exhaustively checking the frequency of each response. It's like manually counting how many times each response appears in the whole set of responses. Finally, we identify the one that showed up the most.
Here's how the algorithm would work step-by-step:
def find_most_common_response_brute_force(responses):
most_common_response = None
maximum_count = 0
# Iterate through each response in the input list.
for current_response in responses:
current_response_count = 0
# Count the occurrences of the current response.
for response in responses:
if response == current_response:
current_response_count += 1
# Update the most common response if the current response's count is higher.
if current_response_count > maximum_count:
maximum_count = current_response_count
most_common_response = current_response
return most_common_response
The goal is to figure out which answer appears most often in a list. A clever approach is to count how many times each answer occurs, and then simply pick the answer with the highest count. This avoids comparing every answer to every other answer.
Here's how the algorithm would work step-by-step:
def find_most_common_response(list_of_responses):
response_tallies = {}
# Tally each response to count occurrences.
for response in list_of_responses:
if response in response_tallies:
response_tallies[response] += 1
else:
response_tallies[response] = 1
most_common_response = None
highest_tally = 0
# Find the response with the highest tally.
for response, tally in response_tallies.items():
# Update the most common response.
if tally > highest_tally:
most_common_response = response
highest_tally = tally
return most_common_response
Case | How to Handle |
---|---|
Null or undefined input list | Return an appropriate error value or throw an exception if the input is null or undefined, as no processing is possible. |
Empty input list | Return a default value indicating no most common response was found such as null, empty string or appropriate error. |
List with all identical responses | The solution should correctly identify and return the repeated response as the most common. |
Maximum-sized input list exceeding available memory | Consider a streaming approach or chunking the data if the input list is too large to fit in memory. |
Multiple responses with the same highest frequency | Return one of the responses arbitrarily or all responses if problem requires it (e.g., return a list). |
Input list contains non-comparable data types | Ensure input is homogenous and comparable or convert/filter as appropriate. |
Case sensitivity for string responses | Normalize the casing (e.g., to lowercase) before counting occurrences to treat different cases as the same response if needed. |
Input contains very long strings or very large numbers | Ensure the counting mechanism can handle large strings/numbers to prevent hash collisions or overflow errors. |