Task: given an array of lowercase words, return every character that appears in all of them, including duplicates (if "l" appears twice in every word, return it twice).
Approach: keep a 26-slot minimum-count array. Start it from the first word's counts, then for every other word take the element-wise minimum. Whatever remains is the multiset common to all words.
from collections import Counter
def commonChars(words):
common = Counter(words[0])
for w in words[1:]:
common &= Counter(w) # & keeps the min count per key
return list(common.elements())
Complexity: O(total characters) time, O(1) extra space for the fixed alphabet.
What interviewers usually probe: - Why intersection is a minimum, not a boolean (duplicates matter). - Unicode follow-up: swap the fixed array for a hash map. - Early exit when the running counter becomes empty.
Disclosure: I build Linkjob AI, an AI interview copilot that helps during live coding rounds. More at https://www.linkjob.ai/interview-copilot/ (AI interview copilot).