String Reordering
Character Manipulation
Algorithms
Programming
String Processing

Reorder a string by half the character

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

Reordering a string by half usually means splitting the string into two halves and then combining them in a new order. The first thing to settle is the exact rule, because "reorder by half" can mean interleaving two halves, swapping them, or treating odd-length strings in a special way.

Define the transformation clearly

One common interpretation is to split the string into two halves and interleave the characters:

  • input: abcdefgh
  • halves: abcd and efgh
  • result: aebfcgdh

For odd-length strings, you also need a rule for the extra character. A simple choice is to keep the extra character in the first half.

A straightforward Python implementation

The function below uses that exact rule:

python
1def reorder_by_half(text: str) -> str:
2    mid = (len(text) + 1) // 2
3    first = text[:mid]
4    second = text[mid:]
5
6    result = []
7    for i in range(len(second)):
8        result.append(first[i])
9        result.append(second[i])
10
11    if len(first) > len(second):
12        result.append(first[-1])
13
14    return "".join(result)
15
16
17print(reorder_by_half("abcdefgh"))
18print(reorder_by_half("abcde"))

Output:

python
aebfcgdh
adbec

This solution is simple and works in linear time because it visits each character once.

Why the midpoint matters

The midpoint decides how odd-length strings behave. In the example above, (len(text) + 1) // 2 gives the first half the extra character.

If you change the split to len(text) // 2, the second half gets the extra character instead, and the output order changes. That is why the problem statement should always define the odd-length rule before implementation starts.

An index-based version

If you want the logic written more explicitly, you can construct the result by index:

python
1def reorder_by_half_indices(text: str) -> str:
2    mid = (len(text) + 1) // 2
3    chars = []
4
5    for i in range(mid):
6        chars.append(text[i])
7        second_index = i + mid
8        if second_index < len(text):
9            chars.append(text[second_index])
10
11    return "".join(chars)
12
13
14print(reorder_by_half_indices("abcdefgh"))

This version avoids temporary slices, though in normal Python code the first version is often easier to read.

Complexity and testing

The interleaving algorithm runs in O(n) time and uses O(n) extra space for the result buffer. That is usually the right tradeoff because Python strings are immutable, so building the result in a list and joining once is faster than repeated string concatenation.

It is also worth testing a few edge cases:

  • the empty string
  • a one-character string
  • even-length input
  • odd-length input

Those cases confirm whether your midpoint rule matches the intended output. They also protect you from off-by-one mistakes, which are the most common bug in half-splitting string code.

Alternative interpretation: swap the halves

Sometimes the real requirement is not interleaving at all. It is only:

  • input: abcdefgh
  • output: efghabcd

That is a different operation:

python
def swap_halves(text: str) -> str:
    mid = len(text) // 2
    return text[mid:] + text[:mid]

Because the phrase is ambiguous, it is worth confirming the expected output with one example before you code the algorithm.

Common Pitfalls

  • Starting to code before defining what "reorder by half" means for odd-length strings.
  • Confusing interleaving two halves with simply swapping the halves.
  • Writing index logic that goes out of bounds on the shorter half.
  • Building strings by repeated concatenation in a loop instead of collecting characters and joining once.

Summary

  • Reordering by half usually starts with splitting the string into two halves and defining a clear recombination rule.
  • Interleaving the halves is a common interpretation and runs in linear time.
  • Odd-length strings need an explicit midpoint rule.
  • Confirm the expected transformation with concrete examples before implementing it.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.