Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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 summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[["good","ok","good","ok"],["ok","bad","good","ok","ok"],["good"],["bad"]]
[["good","ok","good"],["ok","bad"],["bad","notsure"],["great","good"]]
majority-element)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3527: Find the Most Common Response
class Solution {
public String findCommonResponse(List<List<String>> responses) {
Map<String, Integer> cnt = new HashMap<>();
for (var ws : responses) {
Set<String> s = new HashSet<>();
for (var w : ws) {
if (s.add(w)) {
cnt.merge(w, 1, Integer::sum);
}
}
}
String ans = responses.get(0).get(0);
for (var e : cnt.entrySet()) {
String w = e.getKey();
int v = e.getValue();
if (cnt.get(ans) < v || (cnt.get(ans) == v && w.compareTo(ans) < 0)) {
ans = w;
}
}
return ans;
}
}
// Accepted solution for LeetCode #3527: Find the Most Common Response
func findCommonResponse(responses [][]string) string {
cnt := map[string]int{}
for _, ws := range responses {
s := map[string]struct{}{}
for _, w := range ws {
if _, ok := s[w]; !ok {
s[w] = struct{}{}
cnt[w]++
}
}
}
ans := responses[0][0]
for w, v := range cnt {
if cnt[ans] < v || (cnt[ans] == v && w < ans) {
ans = w
}
}
return ans
}
# Accepted solution for LeetCode #3527: Find the Most Common Response
class Solution:
def findCommonResponse(self, responses: List[List[str]]) -> str:
cnt = Counter()
for ws in responses:
for w in set(ws):
cnt[w] += 1
ans = responses[0][0]
for w, x in cnt.items():
if cnt[ans] < x or (cnt[ans] == x and w < ans):
ans = w
return ans
// Accepted solution for LeetCode #3527: Find the Most Common Response
fn find_common_reponse(responses: Vec<Vec<String>>) -> String {
use std::collections::{HashMap, HashSet};
responses
.into_iter()
.map(|v| v.into_iter().collect::<HashSet<_>>())
.fold(HashMap::new(), |mut acc, s| {
for w in s {
*acc.entry(w).or_insert(0) += 1;
}
acc
})
.into_iter()
.fold(("".to_string(), 0), |(ret, max), (k, v)| {
if v > max || (v == max && k < ret) {
(k, v)
} else {
(ret, max)
}
})
.0
}
fn main() {
let responses = vec![
vec![
"good".to_string(),
"ok".to_string(),
"good".to_string(),
"ok".to_string(),
],
vec![
"ok".to_string(),
"bad".to_string(),
"good".to_string(),
"ok".to_string(),
"ok".to_string(),
],
vec!["good".to_string()],
vec!["bad".to_string()],
];
let ret = find_common_reponse(responses);
println!("ret={ret}");
}
#[test]
fn test() {
{
let responses = vec![
vec![
"good".to_string(),
"ok".to_string(),
"good".to_string(),
"ok".to_string(),
],
vec![
"ok".to_string(),
"bad".to_string(),
"good".to_string(),
"ok".to_string(),
"ok".to_string(),
],
vec!["good".to_string()],
vec!["bad".to_string()],
];
let ret = find_common_reponse(responses);
assert_eq!(ret, "good");
}
{
let responses = vec![
vec!["good".to_string(), "ok".to_string(), "good".to_string()],
vec!["ok".to_string(), "bad".to_string()],
vec!["bad".to_string(), "notsure".to_string()],
vec!["great".to_string(), "good".to_string()],
];
let ret = find_common_reponse(responses);
assert_eq!(ret, "bad");
}
}
// Accepted solution for LeetCode #3527: Find the Most Common Response
function findCommonResponse(responses: string[][]): string {
const cnt = new Map<string, number>();
for (const ws of responses) {
const s = new Set<string>();
for (const w of ws) {
if (!s.has(w)) {
s.add(w);
cnt.set(w, (cnt.get(w) ?? 0) + 1);
}
}
}
let ans = responses[0][0];
for (const [w, v] of cnt) {
const best = cnt.get(ans)!;
if (best < v || (best === v && w < ans)) {
ans = w;
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.