One-Mismatch Consensus String
Updated
Problem statement
You are given N words in words. Every word contains exactly K lowercase English letters.
Return a string of length K that differs from every input word at no more than one position. Characters are compared at matching positions; rearranging letters is not allowed.
For this exercise, assume the lexicographically smallest valid string is returned when several exist. For this exercise, assume the answer is an empty string when no valid length-K string exists.
Function
consensusString(words: String[]) → StringExamples
Example 1
words = ["ab","ba"]return = "aa"Both mixed strings aa and bb work; aa is smaller.
Example 2
words = ["abc","axc","aby"]return = "abc"Differences occur at two distinct positions, forcing abc.
Example 3
words = ["abc","xyz"]return = ""The words differ at three positions, so no answer can be within one of both.
Example 4
words = ["zzz","zzz"]return = "azz"Changing the first character to a produces the smallest valid word.
Constraints
2 <= N <= 10^51 <= K <= 10^52 <= N * K <= 10^6- Every word has length
Kand contains only lowercase English letters.
More IMC Trading Online Assessment questions
- Earliest Meeting Start on a 24-Hour ClockOA · Seen Nov 2026
- Grid Paths with Override PassesOA · Seen Oct 2026
- Weighted LFU CacheOA · Seen Sep 2026
- Rock JumpingOA · Seen Aug 2026
- Stack Batch RemovalOA · Seen Aug 2026
- Chain of CommandOA · Seen Aug 2026
- Choose ContainersOA · Seen Aug 2026
- Sub-matrix SumsOA · Seen Jul 2026
- Sub-matrix SumsOA · Seen Jul 2026
- Can Every Package Fit in a BoxOA · Seen Jul 2026
Similar questions at other companies
- Find a String Within One Mismatch of AllBarclays · OA
- Count Subarrays Matching a Comparison PatternTikTok · OA
- Earliest Time to Schedule a MeetingMathWorks · OA
- Compress Editor Actions into Operational Transform OperationsReplit · OA · PHONE SCREEN · ONSITE INTERVIEW
- Count One-Swap Number PairsCapital One · OA
- Earliest Time to Schedule a MeetingMicrosoft · OA
- Nested Recipe Blocks (Game Adaptation)Roblox · OA
- Maximize Workday EarningsCitadel · OA · PHONE SCREEN · ONSITE INTERVIEW
- Maximum Equal Parts for PrefixesAmazon · OA
- Best TV Show in a GenreAdyen · OA