FastPrepFind a String Within One Mismatch of All
Problems/Find a String Within One Mismatch of All

Find a String Within One Mismatch of All

Barclays logoBarclays● MediumINTERNOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Barclays internship online assessment. The task was to find one string that is at most one letter away from every string in a list, or return an empty string when there is none.

The report only explains the idea and the steps to solve it. It gives no examples, limits or function name, and it does not say which answer to return when several work. Here, strs[0] is returned when it works. Otherwise, the answer is the one that changes strs[0] at the smallest index, which is also what the shared steps return. We chose equal lengths, lowercase letters, up to 1000 strings and up to 100 letters, and wrote all the examples.

Our version is about 70% the same as the reported question.

Given an array of strings strs where every string has the same length, return a string of that length that differs from each string in strs in at most one position. If no such string exists, return an empty string "".

Two strings differ in a position when their characters at that index are not equal. Characters are compared index by index and are never moved.

When more than one string works, choose this way:

  • If strs[0] works, return strs[0].
  • Otherwise, every string that works differs from strs[0] in exactly one position. Return the one whose differing position has the smallest index.

Note: in the second case, no two working strings differ from strs[0] at the same index, so the answer is always unique.

Function

findCloseString(strs: String[]) → String

Examples

Example 1

strs = ["abc","abd","xbc"]return = "abc"

strs[0] = "abc" differs from "abd" only at index 2 and from "xbc" only at index 0. So strs[0] itself works, and it is returned.

Example 2

strs = ["abcd","abxy","abcy"]return = "abcy"

strs[0] = "abcd" differs from "abxy" at indices 2 and 3, so it does not work. A working string must then copy one of those two letters from "abxy".

  • Setting index 2 to "x" gives "abxd", which differs from "abcy" at indices 2 and 3. It does not work.
  • Setting index 3 to "y" gives "abcy". It differs from "abcd" and from "abxy" in one position each, and it equals "abcy". It works.

Example 3

strs = ["cat","cog"]return = "cot"

"cat" and "cog" differ at indices 1 and 2. Both "cot" and "cag" are one position away from each of them. "cot" differs from strs[0] at index 1, which is smaller than index 2, so the answer is "cot".

Example 4

strs = ["ab","ba","aa","bb"]return = ""

To be at most one position away from both "ab" and "ba", a string must be "aa" or "bb". But "aa" differs from "bb" in two positions, and "bb" differs from "aa" in two positions. No string works.

Constraints

  • 1 <= strs.length <= 1000
  • 1 <= strs[i].length <= 100
  • All strings in strs have the same length.
  • strs[i] consists of lowercase English letters.

More Barclays Online Assessment questions

  • Allocate Workers for a Production RatioOA · Seen Aug 2026
  • Minimum Steps RequiredOA · Seen Apr 2024
  • Find Maximum Possible GCD 🥭OA · Seen Mar 2024

Similar questions at other companies

  • Count Subarrays Matching a Comparison PatternTikTok · OA
  • Earliest Time to Schedule a MeetingMathWorks · OA
  • Earliest Meeting Start on a 24-Hour ClockIMC Trading · 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
See Barclays hiring insights
Saved
class Solution {
    public String findCloseString(String[] strs) {
        // Write your code here.
    }
}
Checking account…