Skip to content
CodeKruPro

Reverse a string

Coding problemEasyAsked in: Accenture, Capgemini, Cognizant, Infosys, Wipro

Given a string s, return its characters in reverse order.

Write the loop yourself. Do not call a ready-made reverse function or a one-line library shortcut: the interviewer wants to see how you walk the characters.

Spaces, digits and symbols are characters like any other and move too.

Examples

Example 1
Input: s = "hello"
Output: "olleh"
Example 2
Input: s = "ab cd"
Output: "dc ba"
The space moves with the other characters.
Example 3
Input: s = ""
Output: ""

Constraints

  • The length of s is between 0 and 100,000
  • s holds printable ASCII characters only

Run checks the 3 examples. Submit also runs the hidden tests: edge cases, large inputs and a time limit.

Run your code to see the results here.

to answer and keep your progress. Reading is free for everyone.