Swap Adjacent Character Pairs

Reported by candidates from Hartford Financial Services's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Hartford Financial Services OA. Under 2s to a working solution.
Founder's read

Hartford Financial Services reported this one in March 2019, and the input goes up to 10^5 characters. That size matters. If you build the string by repeated concatenation or slicing, you can quietly turn a linear job into a quadratic one. The task is plain string manipulation: swap each non-overlapping pair, leave the last character alone if the length is odd. It's an easy problem that punishes sloppy string handling. If you blank during the live OA, StealthCoder runs invisibly as a safety net and hands you the clean solution. Here's the pattern and the traps.

The problem

Given a string text, swap the two characters in every consecutive, nonoverlapping pair, starting from the beginning, and return the resulting string.
The pairs use zero-based positions (0, 1), (2, 3), and so on. If the length is odd, leave the final unpaired character unchanged. Preserve the order of the pairs.
For example, abcd becomes badc.

Function
swapAdjacentPairs(text: String) → String

Examples
Example 1
text = "abcd"
return = "badc"
Swap a with b, then c with d. This input/output pair appears in the report.
Example 2
text = "abcde"
return = "badce"
The pairs are swapped, while the final e has no partner and stays unchanged.
Example 3
text = "a"
return = "a"
There is no complete pair, so the string stays unchanged.

Constraints
1 <= text.length <= 10^5.
text contains only lowercase English letters a through z.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is one pass with a step of 2. Convert the string to a character array or list, then for i from 0 while i+1 < n, swap chars at i and i+1, and advance i by 2. Join at the end. That's O(n) time and O(n) space. The pitfall is performance. In languages with immutable strings, doing text = text[:i] +... inside a loop copies the whole string every time, and at 10^5 characters that hurts. The second pitfall is the odd length. The i+1 < n check handles it, so the last character stays put. Length 1 falls out of the same check with no special case. Don't overthink it. If the clock or nerves get to you, StealthCoder is the hedge on the live OA, giving you the working loop while you verify it against abcd, abcde and a.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

You can drill Swap Adjacent Character Pairs cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Hartford Financial Services's OA.

Hartford Financial Services reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Swap Adjacent Character Pairs FAQ

How hard is Swap Adjacent Character Pairs really?+

It's easy. One loop, step by 2, swap neighbors. The only real risk is a careless off-by-one on odd lengths or building the result with repeated concatenation. If you can write a for loop and a bounds check, you can finish this in a few minutes.

What's the trick to handle odd-length strings?+

Loop while i + 1 < n, stepping i by 2. When the length is odd, the last index fails the check and the loop ends, so the final character is left untouched. A single-character string also passes through unchanged with no special case.

Why does the 10^5 length limit matter here?+

It rules out quadratic string building. Slicing and concatenating inside a loop copies the string each time in many languages. Use a mutable array or a list of characters, then join once at the end. That keeps it linear and safe at 10^5.

Can I solve it without extra space?+

Strings are immutable in most languages, so you'll need an O(n) character array anyway. In a language with mutable strings you can swap in place with O(1) extra space. For the OA, the array approach is fine and easier to get right.

How do I prepare for this in 48 hours?+

Practice stepping through a string by pairs and swapping with a bounds check. Test three cases by hand: even length, odd length, and length 1. Also be comfortable converting between string and character array in your language. That covers everything this problem tests.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Hartford Financial Services.

OA at Hartford Financial Services?
Invisible during screen share
Get it