Reverse Only Letters

EasyTwo Pointerstwo pointers

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Given a string s, reverse the order of its English letters (a-z and A-Z) while every non-letter character stays at the exact index where it started. Return the resulting string. Letters keep their case.

Examples

Input: s = "ab-cd"
Output: "dc-ba"
The hyphen stays at index 2.
Input: s = "a-bC-dEf-ghIj"
Output: "j-Ih-gfE-dCba"
Hyphens stay put while letters reverse.
Input: s = "Test1ng-Leet=code-Q!"
Output: "Qedo1ct-eeLg=ntse-T!"
Digits and punctuation keep their positions.

Constraints

  • 1 <= s.length <= 100
  • `s` consists of ASCII characters with codes in `[33, 122]`
  • `s` does not contain `"` or `\`

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More two pointers problems