FastPrepSwap Adjacent Character Pairs
Problems/Swap Adjacent Character Pairs

Swap Adjacent Character Pairs

Hartford Financial Services logoHartford Financial Services● EasyFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report gives only abcd → badc. We assume adjacent pairs are swapped and a last odd character stays in place. Our version is about 75% the same as that example.

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.

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.

More Hartford Financial Services interview questions

  • Maximum Product SubarrayONSITE INTERVIEW · Seen Sep 2026
  • Bubble SortONSITE INTERVIEW · Seen Mar 2019
  • Assign Student Grades from CSV MarksONSITE INTERVIEW
  • Second Highest SalaryONSITE INTERVIEW
  • Average Grades per StudentPHONE SCREEN · Seen Aug 2026

Similar questions at other companies

  • Christmas Tree with N TiersVerily · ONSITE INTERVIEW
  • Longest Common PrefixKotak Mahindra Bank · PHONE SCREEN · ONSITE INTERVIEWKotak Mahindra Bank interview questions
  • Valid ParenthesesMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • Count Adjacent Symbol Pairs for BPEAmazon · ONSITE INTERVIEW
  • Longest Spoken PrefixElevenLabs · ONSITE INTERVIEWElevenLabs interview questions
  • Palindrome Check Ignoring SymbolsWalmart · ONSITE INTERVIEWWalmart interview questions
  • Reverse a String Keeping Dashes in PlaceNykaa · ONSITE INTERVIEWNykaa interview questions
  • Implement tail -nConfluent · PHONE SCREEN · ONSITE INTERVIEWConfluent interview questions
  • Valid ParenthesesEPAM · ONSITE INTERVIEWEPAM interview questions
  • Map and Accumulate Learning QueriesPalantir · ONSITE INTERVIEWPalantir interview questions
All Hartford Financial Services interview questionsSee Hartford Financial Services hiring insights
Saved
class Solution {
    public String swapAdjacentPairs(String text) {
        // write your code here
    }
}
Checking account…