Combine a string with its reverse and replace every space with %20

Quick Overview

Build a new string by appending the reverse of the input to itself, then replace every space with the URL-encoded sequence %20. It tests string reversal, the order of two transformations and building strings in linear time.

Combine a string with its reverse and replace every space with %20

Company: Miro

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given a string `s`, perform the following two operations in order and return the resulting string: 1. Combine: append the reverse of `s` to the end of `s`. 2. Replace: in the combined string, replace every space character (`' '`) with the URL-encoded sequence `"%20"`. Return the final transformed string. ### Function Signature ```python def combine_and_replace(s: str) -> str: ... ``` ### Rules - The reverse of `s` is its characters in reverse order, spaces included. - The replacement runs on the combined string, so each space in `s` produces two `%20` sequences in the result. Every other character is copied unchanged. ### Constraints - `1 <= len(s) <= 100` - `s` consists of lowercase English letters and spaces only. - The combined string after replacement will not exceed 300 characters. ### Examples ```text Input: s = "hello world" Output: "hello%20worlddlrow%20olleh" ``` The reverse of "hello world" is "dlrow olleh", so the combined string is "hello worlddlrow olleh". Each of its two spaces becomes `%20`. ```text Input: s = "abc" Output: "abccba" ``` The reverse is "cba" and the combined string "abccba" has no spaces, so nothing is replaced. ```text Input: s = " a b" Output: "%20a%20bb%20a%20" ``` The reverse is "b a ", so the combined string is " a bb a " with four spaces, including one at each end. The result has 8 + 4 * 2 = 16 characters.

Overview: Build a new string by appending the reverse of the input to itself, then replace every space with the URL-encoded sequence %20. It tests string reversal, the order of two transformations and building strings in linear time.

Miro logo
Miro
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given a string s, perform the following two operations in order and return the resulting string:

  1. Combine: append the reverse of s to the end of s .
  2. Replace: in the combined string, replace every space character ( ' ' ) with the URL-encoded sequence "%20" .

Return the final transformed string.

Function Signature

def combine_and_replace(s: str) -> str:
    ...

Rules

  • The reverse of s is its characters in reverse order, spaces included.
  • The replacement runs on the combined string, so each space in s produces two %20 sequences in the result. Every other character is copied unchanged.

Constraints

  • 1 <= len(s) <= 100
  • s consists of lowercase English letters and spaces only.
  • The combined string after replacement will not exceed 300 characters.

Examples

Input: s = "hello world"
Output: "hello%20worlddlrow%20olleh"

The reverse of "hello world" is "dlrow olleh", so the combined string is "hello worlddlrow olleh". Each of its two spaces becomes %20.

Input: s = "abc"
Output: "abccba"

The reverse is "cba" and the combined string "abccba" has no spaces, so nothing is replaced.

Input: s = " a b"
Output: "%20a%20bb%20a%20"

The reverse is "b a ", so the combined string is " a bb a " with four spaces, including one at each end. The result has 8 + 4 * 2 = 16 characters.