Bird
Raised Fist0
Interview Preptwo-pointerseasyAmazonFacebookGoogle

Backspace String Compare

Choose your preparation mode4 modes available
📋
Problem

Imagine typing a message on your phone where you often hit backspace. How can you verify if two typed messages are actually the same after considering all backspaces?

Given two strings S and T, return true if they are equal when both are typed into empty text editors. '#' means a backspace character. If a backspace is encountered, it deletes the character before it if any. Return true if the final strings are equal, otherwise false.

1 ≤ length of S, T ≤ 10^5S and T consist of lowercase letters and '#' characters only
Edge cases: Both strings empty → trueStrings with only backspaces → trueOne string empty, other non-empty → false
</>
IDE
def backspaceCompare(S: str, T: str) -> bool:public boolean backspaceCompare(String S, String T)bool backspaceCompare(string S, string T)function backspaceCompare(S, T)
def backspaceCompare(S, T):
    # Write your solution here
    pass
class Solution {
    public boolean backspaceCompare(String S, String T) {
        // Write your solution here
        return false;
    }
}
#include <string>
using namespace std;

bool backspaceCompare(string S, string T) {
    // Write your solution here
    return false;
}
function backspaceCompare(S, T) {
    // Write your solution here
}
Coming soon
0/9
Common Bugs to Avoid
Wrong: falseIncorrectly simulating backspaces, missing characters that should be removed.✅ Use a stack or two-pointer approach to simulate backspaces correctly before comparing strings.
Wrong: trueReturning true when one string is empty and the other is not after backspace processing.✅ Compare final processed strings' lengths and contents before returning true.
Wrong: trueGreedy removal of characters without proper backspace simulation causing incorrect equality.✅ Simulate backspaces fully using two pointers or stack rather than greedy removal.
Wrong: falseOff-by-one errors removing wrong characters especially at string start.✅ Check index bounds carefully before removing characters during backspace simulation.
Wrong: TLERebuilding strings repeatedly or using inefficient nested loops causing timeouts.✅ Use two-pointer approach from the end to achieve O(n) time complexity.
✓
Test Cases
t1_01basic
Input{"S":"ab#c","T":"ad#c"}
Expectedtrue

Both strings become 'ac' after processing backspaces, so they are equal.

t1_02basic
Input{"S":"ab##","T":"c#d#"}
Expectedtrue

Both strings become empty after processing backspaces, so they are equal.

t2_01edge
Input{"S":"","T":""}
Expectedtrue

Both strings are empty, so they are trivially equal.

t2_02edge
Input{"S":"######","T":"###"}
Expectedtrue

Both strings contain only backspaces and thus become empty after processing.

t2_03edge
Input{"S":"","T":"a"}
Expectedfalse

One string is empty, the other is not, so they cannot be equal.

t3_01corner
Input{"S":"a#c","T":"b"}
Expectedfalse

After processing, S becomes 'c' and T is 'b', which are not equal.

t3_02corner
Input{"S":"bxj##tw","T":"bxo#j##tw"}
Expectedtrue

Both strings become 'btw' after processing backspaces.

t3_03corner
Input{"S":"nzp#o#g","T":"b#nzp#o#g"}
Expectedtrue

Both strings become 'nzog' after processing backspaces.

t4_01performance
Input{"_description":"n=100000 at constraint boundary - executor generates this"}
⏱ Performance - must finish in 2000ms

Input size n=100000 tests O(n) time complexity solutions to complete within 2 seconds.

Practice

(1/5)
1. Consider the following code snippet implementing the 3Sum Closest algorithm. What is the returned value when the input is nums = [-1, 2, 1, -4] and target = 1?
def threeSumClosest(nums, target):
    nums.sort()
    n = len(nums)
    closest_sum = nums[0] + nums[1] + nums[2]
    for i in range(n - 2):
        left, right = i + 1, n - 1
        while left < right:
            current_sum = nums[i] + nums[left] + nums[right]
            if current_sum == target:
                return current_sum
            if abs(current_sum - target) < abs(closest_sum - target):
                closest_sum = current_sum
            if current_sum < target:
                left += 1
            else:
                right -= 1
    return closest_sum

print(threeSumClosest([-1, 2, 1, -4], 1))
easy
A. 1
B. 4
C. -1
D. 2

Solution

  1. Step 1: Sort the array

    Sorted nums = [-4, -1, 1, 2]
  2. Step 2: Trace iterations

    i=0: left=1, right=3 current_sum = -4 + (-1) + 2 = -3 (closest_sum init = -4+-1+1 = -4) Update closest_sum to -3 current_sum < target(1), left++ -> left=2 current_sum = -4 + 1 + 2 = -1 Update closest_sum to -1 current_sum < target, left++ -> left=3 (loop ends) i=1: left=2, right=3 current_sum = -1 + 1 + 2 = 2 abs(2-1)=1 < abs(-1-1)=2, update closest_sum=2 current_sum > target, right-- -> right=2 (loop ends) Return closest_sum = 2
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Output matches expected closest sum 2 [OK]
Hint: Sorting and pointer moves yield closest sum 2 [OK]
Common Mistakes:
  • Returning initial sum without updates
  • Off-by-one pointer increments
  • Confusing current_sum with closest_sum
2. What is the time and space complexity of the optimal two-pointer overwrite approach for moving zeroes to the end of an array of length n?
medium
A. Time: O(n), Space: O(1) because it scans array twice but no extra space
B. Time: O(n^2), Space: O(1) due to nested loops
C. Time: O(n), Space: O(n) because it uses an auxiliary array implicitly
D. Time: O(n log n), Space: O(1) due to sorting involved

Solution

  1. Step 1: Identify loops and operations

    The algorithm scans the array twice sequentially: once to overwrite non-zero elements, once to fill zeroes.
  2. Step 2: Analyze time and space

    Each loop is O(n), total O(n). No extra arrays or recursion used, so space is O(1).
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Two linear passes, constant extra space [OK]
Hint: Two linear scans, no nested loops or extra arrays [OK]
Common Mistakes:
  • Mistaking two loops as nested (O(n^2))
  • Assuming extra space due to overwriting
  • Confusing with sorting complexity
3. The following code attempts to move zeroes to the end of the array in-place:
def moveZeroes(nums):
    lastNonZeroFoundAt = 0
    for num in nums:
        if num != 0:
            nums[lastNonZeroFoundAt] = num
            lastNonZeroFoundAt += 1
    # Bug introduced here
    for i in range(lastNonZeroFoundAt, len(nums)):
        nums[i] = nums[i]
What is the bug in this code?
medium
A. The variable lastNonZeroFoundAt is incremented incorrectly.
B. The first loop does not correctly identify non-zero elements.
C. The second loop does not fill zeroes after lastNonZeroFoundAt, leaving old values intact.
D. The assignment inside the first loop overwrites zeroes incorrectly.

Solution

  1. Step 1: Analyze second loop behavior

    The second loop assigns nums[i] = nums[i], which does nothing and fails to fill zeroes.
  2. Step 2: Identify correct fix

    It should assign nums[i] = 0 to move zeroes to the end properly.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Zeroes remain unfilled, causing incorrect output [OK]
Hint: Second loop must fill zeroes, not copy existing values [OK]
Common Mistakes:
  • Forgetting to fill zeroes after moving non-zero elements
  • Misunderstanding loop purpose
4. Suppose the problem changes so that elements can be reused multiple times in triplets (i.e., indices can repeat). Which modification to the two-pointer approach correctly counts the number of triplets with sum less than target?
hard
A. Keep the same two-pointer approach but allow left and right pointers to be equal and count accordingly.
B. Use three nested loops to consider all triplets with possible repeated indices.
C. Sort the array and for each i, use two pointers starting at i to allow reuse, adjusting counting logic accordingly.
D. Sort the array and for each i, use a single pointer to count pairs with sum less than target minus nums[i], allowing reuse.

Solution

  1. Step 1: Understand reuse of elements

    Allowing repeated indices means pairs can include the same element multiple times, so two-pointer approach must adapt.
  2. Step 2: Adjust approach for reuse

    Using a single pointer to count pairs with sum less than target - nums[i] allows counting with reuse, since two pointers crossing is no longer valid.
  3. Step 3: Why other options fail

    Allowing left == right breaks two-pointer logic (A). Three nested loops (B) is correct but inefficient. Starting two pointers at i (C) is incorrect because it counts invalid pairs.
  4. Final Answer:

    Option D -> Option D
  5. Quick Check:

    Single pointer counting with reuse is correct and efficient [OK]
Hint: Reuse requires counting pairs with single pointer or adjusted logic [OK]
Common Mistakes:
  • Allowing pointers to cross causing invalid counts
  • Using naive triple loops ignoring efficiency
  • Starting two pointers incorrectly leading to missed counts
5. Suppose the array is not sorted and you want to remove duplicates in-place with O(1) extra space. Which modification to the two-pointer approach is correct?
hard
A. Scan the array multiple times with nested loops to remove duplicates.
B. Use two pointers without sorting, comparing elements directly to remove duplicates.
C. Use a hash set to track duplicates and overwrite duplicates in-place.
D. Sort the array first, then apply the two-pointer approach.

Solution

  1. Step 1: Understand constraints

    Two-pointer approach relies on sorted array to detect duplicates in one pass.
  2. Step 2: Identify correct modification

    Sorting first ensures duplicates are adjacent, enabling two-pointer removal in O(n log n) time and O(1) space.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Sorting enables two-pointer approach on unsorted input [OK]
Hint: Two-pointer requires sorted array; sort first if unsorted [OK]
Common Mistakes:
  • Trying two-pointer without sorting
  • Using extra space violating constraints
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard