InternDoor

Reverse the letters, leave the rest

Easystrings · two pointers

Reported in interviews at (where)

  • Spire Technologies

Reverse the order of the letters in s, the characters a to z and A to Z, while every other character (digits, punctuation, spaces) stays exactly where it is.

Return the result.

Example 1
Input
s = "Ab,c$de"
Output
"ed,c$bA"

The letters A, b, c, d, e come back as e, d, c, b, A in the same five places; "," and "$" stay put.

Example 2
Input
s = "7-up!"
Output
"7-pu!"

Only u and p swap.

Limits

  • 1 <= s.length <= 100000
  • s holds printable ASCII characters

Hints

Hint 1

Move two pointers inwards. Each one steps over anything that is not a letter; when both stand on letters, swap them.

Where this was reported

Our own problem on the task these candidates describe; the wording, examples and tests are InternDoor’s.

Run your code to check it on the examples. Submit runs every hidden test.

Ready for the real one?

Engineering internships and entry-level jobs in India, newest first.