Reverse, count words, trim and reverse each word in a string without built-in string functions

Quick Overview

Implement four string operations by hand: reverse a string, count its words, trim leading and trailing spaces, and reverse each word while keeping word order. It tests careful index handling, word-boundary detection and edge cases such as repeated spaces or input made only of spaces.

Reverse, count words, trim and reverse each word in a string without built-in string functions

Company: NVIDIA

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given a string `s`, perform four string operations without using any built-in string library functions (no `split`, `reverse`, `strip`, `join` and so on). Implement every operation from scratch by iterating over the string character by character. Return a list of four strings: 1. The reversed string of `s`. 2. The number of words in `s`, written as a string. Words are separated by one or more spaces. 3. The trimmed string, with all leading and trailing spaces removed. 4. The string with each word reversed in place (word order preserved, words separated by single spaces, no leading or trailing spaces). A word is a maximal sequence of non-space characters. ### Function Signature ```python def manipulate_string(s: str) -> list[str]: ``` ### Rules - The result has exactly four entries, in the order listed above. - The word count is written in decimal with no padding, for example `"2"`. - Trimming removes spaces only at the two ends. Spaces between words stay exactly as they appear in `s`. - The only whitespace character is the space `' '`. - If `s` contains only spaces, the word count is `"0"` and both the trimmed string and the word-reversed string are empty. ### Constraints - `1 <= len(s) <= 10^4` - `s` consists of English letters, digits and spaces. ### Examples ```text Input: s = " hello world " Output: [" dlrow olleh ", "2", "hello world", "olleh dlrow"] ``` The full reversal still has two spaces at each end because `s` has two at each end. There are two words, trimming drops the edge spaces, and reversing each word in place gives "olleh dlrow". ```text Input: s = "abc def ghi" Output: ["ihg fed cba", "3", "abc def ghi", "cba fed ihg"] ``` There are no edge spaces, so the trimmed string equals `s`. Reversing each of the three words keeps their order, while the full reversal also flips the word order. ```text Input: s = "a bc " Output: [" cb a", "2", "a bc", "a cb"] ``` Trimming removes only the trailing space and keeps the double space between the words. The word-reversed string puts a single space between "a" and "cb".

Overview: Implement four string operations by hand: reverse a string, count its words, trim leading and trailing spaces, and reverse each word while keeping word order. It tests careful index handling, word-boundary detection and edge cases such as repeated spaces or input made only of spaces.

NVIDIA logo
NVIDIA
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given a string s, perform four string operations without using any built-in string library functions (no split, reverse, strip, join and so on). Implement every operation from scratch by iterating over the string character by character.

Return a list of four strings:

  1. The reversed string of s .
  2. The number of words in s , written as a string. Words are separated by one or more spaces.
  3. The trimmed string, with all leading and trailing spaces removed.
  4. The string with each word reversed in place (word order preserved, words separated by single spaces, no leading or trailing spaces).

A word is a maximal sequence of non-space characters.

Function Signature

def manipulate_string(s: str) -> list[str]:

Rules

  • The result has exactly four entries, in the order listed above.
  • The word count is written in decimal with no padding, for example "2" .
  • Trimming removes spaces only at the two ends. Spaces between words stay exactly as they appear in s .
  • The only whitespace character is the space ' ' .
  • If s contains only spaces, the word count is "0" and both the trimmed string and the word-reversed string are empty.

Constraints

  • 1 <= len(s) <= 10^4
  • s consists of English letters, digits and spaces.

Examples

Input: s = "  hello world  "
Output: ["  dlrow olleh  ", "2", "hello world", "olleh dlrow"]

The full reversal still has two spaces at each end because s has two at each end. There are two words, trimming drops the edge spaces, and reversing each word in place gives "olleh dlrow".

Input: s = "abc def ghi"
Output: ["ihg fed cba", "3", "abc def ghi", "cba fed ihg"]

There are no edge spaces, so the trimmed string equals s. Reversing each of the three words keeps their order, while the full reversal also flips the word order.

Input: s = "a  bc "
Output: [" cb  a", "2", "a  bc", "a cb"]

Trimming removes only the trailing space and keeps the double space between the words. The word-reversed string puts a single space between "a" and "cb".