FastPrepRemove AB and CD Pairs
Problems/Remove AB and CD Pairs

Remove AB and CD Pairs

Hudson River Trading logoHudson River Trading● EasyNEW GRADOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Two posts about a 2021 Hudson River Trading online assessment for new grad Python roles describe this task. You keep deleting AB or CD wherever the two letters sit next to each other, until no such pair is left.

The written reports give no limits or examples, and they do not say which letters can appear or whether the answer is the final string or its length. Here the string has 1 to 2 * 10^5 capital letters, any capital letter can appear, and you return the final string, which can be empty.

Our version is about 85% the same as the reported questions.

You are given a string s of uppercase English letters.

If "AB" or "CD" appears in s as two letters next to each other, delete those two letters. The letters on each side then become neighbors, which can make a new pair. Keep deleting until s contains neither "AB" nor "CD", and return the final string.

Only these two pairs are deleted, and their letters must be in this order. For example, "BA", "DC", "AD" and two equal letters all stay. The final string is the same no matter which pair you delete first. It can be the empty string.

Function

removePairs(s: String) → String

Examples

Example 1

s = "CABDAX"return = "AX"

The only pair at the start is AB. Deleting it leaves "CDAX". Now C and D are next to each other, so delete CD to get "AX".

AX is not a pair, so the answer is "AX".

Example 2

s = "AACDBB"return = ""

At first the only pair is CD in the middle. Deleting it leaves "AABB".

Now the middle letters form AB. Deleting them leaves "AB", and deleting that pair leaves the empty string.

Example 3

s = "BADCACBD"return = "BADCACBD"

The neighbors are BA, AD, DC, CA, AC, CB and BD. None of them is AB or CD.

BA and DC have the right letters in the wrong order, and AD and CB mix the two pairs. Nothing is deleted, so the string stays the same.

Constraints

  • 1 <= s.length <= 2 * 10^5
  • s contains only uppercase English letters 'A' to 'Z'.

More Hudson River Trading Online Assessment questions

  • Count Palindrome Addition ChoicesOA · Seen Nov 2026
  • Count Multiples of Three with Two SevensOA · Seen Oct 2026
  • Recursively Expanded Shell Command CountsOA · Seen Oct 2026
  • Rotate Matrix Regions Around the DiagonalsOA · Seen Oct 2026
  • Maximum Diamond SumOA · Seen Oct 2026
  • Smallest Most-Lit Street PositionOA · Seen Oct 2026
  • Rearrange String by Alternating EndsOA · Seen Sep 2026
  • Most Items Within Budget from a Start IndexOA · Seen Sep 2026
  • Count Sawtooth SubarraysOA · Seen Aug 2026
  • Backtick Identifier ConverterOA · Seen Jun 2026

Similar questions at other companies

  • Validate a Bracket SequenceInMobi · OAInMobi interview questions
  • Valid ParenthesesBlackRock · OABlackRock interview questions
  • Valid ParenthesesVanguard · OAVanguard interview questions
  • Valid Bracket SequenceNVIDIA · OA · PHONE SCREENNVIDIA interview questions
  • Get Final StringIBM · OAIBM interview questions
  • Backspace String CompareGoldman Sachs · OAGoldman Sachs interview questions
  • The PromHackerEarth · OA
  • Compare Character Arrays with BackspacesOdoo · OAOdoo interview questions
  • Eliminate SubstringJP Morgan Chase · OAJP Morgan Chase interview questions
  • Balancing ParenthesesOracle · OAOracle interview questions

Interview reports that mention this question

  • HRT New Grad Python Track Interviews: 9 Experiences

Hudson River Trading interview experiences

  • Hudson River Trading Quant Video Interview: 1 Experience
  • HRT SWE Intern and Onsites: 34 Experiences (Part 2 of 2)
  • HRT Python Track Interviews: 64 Experiences (Part 1 of 2)
All Hudson River Trading interview questionsSee Hudson River Trading hiring insights
Saved
class Solution {
    public String removePairs(String s) {
        // Write your code here.
    }
}
Checking account…