Find the Difference

Algorithms
Easy
Stripe
145.7K views

You are given two strings $s$ and $t$. String $t$ is generated by randomly shuffling string $s$ and then adding one more letter at a random position. Find the letter that was added to $t$. Solve using XOR or frequency array.

Why Interviewers Ask This

Stripe values engineers who write efficient, bug-free code that handles edge cases gracefully. This question evaluates your ability to optimize space complexity beyond brute force while demonstrating logical precision. Interviewers specifically look for candidates who recognize mathematical properties like XOR to solve problems in O(n) time with O(1) space, reflecting a deep understanding of algorithmic fundamentals rather than just syntax.

How to Answer This Question

1. Clarify the problem constraints immediately: confirm if strings contain only lowercase English letters and verify input sizes. 2. Propose two distinct solutions: a frequency array approach for readability and an XOR bitwise operation approach for optimal space efficiency. 3. Walk through the logic of the XOR solution step-by-step, explaining how identical characters cancel each other out (a ^ a = 0), leaving only the unique added character. 4. Analyze both approaches explicitly, comparing their time complexity (O(n)) and space complexity (O(1) vs O(k)). 5. Write clean, syntactically correct code, ensuring you handle variable naming conventions consistent with Stripe's engineering standards. 6. Briefly mention edge cases, such as when the added character is at the very beginning or end of the string.

Key Points to Cover

  • Demonstrating knowledge of bitwise operations to achieve O(1) space complexity
  • Explicitly stating time complexity as O(n) for both proposed solutions
  • Explaining the mathematical cancellation property of the XOR operator clearly
  • Comparing the trade-offs between the frequency array and XOR methods
  • Writing clean, readable code that handles the specific problem constraints

Sample Answer

To solve this efficiently, I first consider the constraints. Since we need to find a single extra character in two strings where one is a shuffled version of the other plus one letter, a brute-force comparison would be too slow. Instead, I recommend the XOR approach for its elegance and constant space usage. The core logic relies on the property that x ^ x equals zero and x ^ 0 equals x. If we XOR all characters from string s and then XOR all characters from string t, every character appearing in both will cancel itself out. The only value remaining will be the ASCII value of the added character. For example, if s is 'abcd' and t is 'abcde', XORing 'a','b','c','d' results in a temporary value. Then XORing 'a','b','c','d','e' cancels the first four, leaving 'e'. This runs in linear time, O(n), because we iterate through both strings once, but uses O(1) auxiliary space since we only store a single integer result. While a frequency map or hash table works, it requires O(k) space where k is the character set size. Given Stripe's focus on performance-critical infrastructure, the XOR method demonstrates superior resource optimization without sacrificing clarity.

Common Mistakes to Avoid

  • Using nested loops to compare characters which results in inefficient O(n^2) time complexity
  • Failing to explain why the XOR method works before implementing it
  • Ignoring the possibility of the added character being at any random position in the string
  • Overlooking the distinction between time complexity and space complexity in the final analysis

Practice This Question with AI

Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.

Start Practicing

Related Interview Questions

Browse all 145 Algorithms questionsBrowse all 57 Stripe questions