Quick Overview

Rearrange a string by taking characters alternately from its front and its back: first, last, second, second-to-last and so on until every character is used once. Tests clean index handling for even and odd lengths and building the result efficiently for long inputs.

Reorder a String by Alternating Its Front and Back Characters

Company: Capital One

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Online Assessment

Given a string `s`, build a new string by taking characters alternately from the front and the back of `s`: the first character, then the last character, then the second character, then the second-to-last character, and so on, until every character of `s` has been used exactly once. ### Function Signature ```python def reorder_front_back(s: str) -> str: ``` ### Rules - Let `n = len(s)`. The output is `s[0]`, `s[n-1]`, `s[1]`, `s[n-2]`, `s[2]`, `s[n-3]`, and so on, stopping as soon as `n` characters have been written. - Every position of `s` is used exactly once, so the output also has length `n`. When `n` is odd, the middle character `s[n // 2]` is the last one written. - Characters are copied unchanged. ### Constraints - `1 <= len(s) <= 10^5` - `s` consists of lowercase English letters. ### Examples **Example 1** ```text Input: s = "abcdef" Output: "afbecd" ``` The characters are taken in the order `a` (first), `f` (last), `b` (second), `e` (second-to-last), `c`, `d`. **Example 2** ```text Input: s = "hello" Output: "hoell" ``` The order is `h` (first), `o` (last), `e` (second), `l` (second-to-last), and finally the middle `l`. **Example 3** ```text Input: s = "x" Output: "x" ```

Overview: Rearrange a string by taking characters alternately from its front and its back: first, last, second, second-to-last and so on until every character is used once. Tests clean index handling for even and odd lengths and building the result efficiently for long inputs.

Given a string `s` of lowercase English letters, build a new string by taking characters alternately from the front and the back of `s`: the first character, then the last character, then the second character, then the second-to-last character, and so on, until every character of `s` has been used exactly once. Implement `reorder_front_back(s)` and return the new string. ### Rules - Let `n = len(s)`. The output is `s[0]`, `s[n-1]`, `s[1]`, `s[n-2]`, `s[2]`, `s[n-3]`, and so on, stopping as soon as `n` characters have been written. - Every position of `s` is used exactly once, so the output also has length `n`. When `n` is odd, the middle character `s[n // 2]` is the last one written. - Characters are copied unchanged. ### Constraints - `1 <= len(s) <= 10^5` - `s` consists of lowercase English letters. No value in this problem can exceed 2^31 - 1: the input and the output are both strings of length `n`. ### Examples **Example 1** ```text Input: s = "abcdef" Output: "afbecd" ``` The characters are taken in the order `a` (first), `f` (last), `b` (second), `e` (second-to-last), `c`, `d`. **Example 2** ```text Input: s = "hello" Output: "hoell" ``` The order is `h` (first), `o` (last), `e` (second), `l` (second-to-last), and finally the middle `l`.

Constraints

  • 1 <= len(s) <= 10^5
  • s consists of lowercase English letters.

Examples

Input: ('x',)

Expected Output: 'x'

Explanation: Singleton (n = 1): the only character is returned unchanged.

Input: ('ab',)

Expected Output: 'ab'

Explanation: Smallest even length: first then last, with no overlap.

Hints

  1. For a small string such as "hello", write down which index of s supplies each output character; the rules fix that index sequence completely.
  2. Think about how many positions remain unused at each end after every step. What should happen when exactly one unused position is left?
  3. The output has exactly len(s) characters, so check that no position is written twice or skipped, especially when the length is odd.

Loading coding console...

Show the approach

Approach

Use two indices: i starts at the front (0) and j at the back (n - 1). While i <= j, write s[i]; if i != j, also write s[j]; then advance i and retreat j. Invariant: after k iterations the output holds exactly s[0], s[n-1], s[1], s[n-2], ..., s[k-1], s[n-k] in the required order, and the unused positions are exactly i..j. Each iteration therefore appends the next characters of the order the rules define. For even n the indices cross after n/2 iterations with no overlap; for odd n the final iteration has i == j, so the middle character s[n // 2] is written once and last. Every position is written exactly once, so the result has length n. Edge cases: n = 1 returns s itself; strings of identical letters and palindromes still consume positions one at a time, so their length is preserved. Collecting characters in a list or builder and joining once keeps the pass linear instead of quadratic string concatenation.

Time complexity:
O(n)
Space complexity:
O(n)