Problem · String
Alternate String Ends
Learn this problemProblem statement
Given a string s, return a new string formed by reading characters from alternating ends.
Use the characters in this order:
- Take the first character.
- Take the last character.
- Take the second character.
- Take the second-to-last character.
- Continue moving inward until every character has been used exactly once.
Equivalently, for a string of length n, visit indices 0, n - 1, 1, n - 2, and so on.
Function
alternateStringEnds(s: String) → StringExamples
Example 1
s = "abcde"return = "aebdc"The characters are taken from indices 0, 4, 1, 3, and 2, producing aebdc.
Example 2
s = "abcdef"return = "afbecd"The characters are taken from indices 0, 5, 1, 4, 2, and 3, producing afbecd.
Example 3
s = "x"return = "x"A one-character string has the same character at the only position, so the result is x.
More Capital One problems
- Compare Counts Around PivotOA · Seen Jul 2026
- Format a Newspaper PageOA · Seen Jul 2026
- Laser Robot Safe PathOA · Seen Jul 2026
- Match Consecutive Word BoundariesOA · Seen Jul 2026
- Reconstruct Landmark JourneyOA · Seen Jul 2026
- Rightmost Longest Character RunOA · Seen Jul 2026
- Track Received Byte RangesOA · Seen Jul 2026
- Round-Robin WDL OrderOA · Seen Jul 2026