Asked at 12 companies
7 recent
- Qualcomm
- tcs
- Bloomberg
- Amazon
- Microsoft
- Meta
- Siemens
- Wipro
- EPAM Systems
+2 moreShow fewer
- HCL
- Accenture
Community-reported · source · updated 2026-11-18. ● asked in the last 6 months.
Go step by step: try it yourself before the video and the visual. Progress saves on this device.
0 / 110 pointsSolved (Easy) 0/20Reviews 0/50Mastered 0/25Practice steps 0/15
Step 1
Know these before you start
Practise each one here, or tick what you already know
Builds on: Indexing and walking through an array · Building a new string or array as the answer · Partitioning around a pivot · Sorting costs O(n log n)
Step 2
Try it on paper
5–10 minutes, no code yet
In the wild · Merging sorted data: Databases join two sorted tables by walking a pointer down each one at once, the same move as merging two sorted arrays.
ProblemProblem statementWrite a function that reverses a string. The input string is given as an array of characters s.ReadHide
Problem Statement
Write a function that reverses a string. The input string is given as an array of characters s.
You must do this by modifying the input array in-place with O(1) extra memory.
""" + PROV + r"""
Input / Output
Input: s = ["h","e","l","l","o"]
Output: ["o","l","l","e","h"]
Input: s = ["H","a","n","n","a","h"]
Output: ["h","a","n","n","a","H"]
Code it here
Tab / Shift+Tab indent · ⌘/Ctrl+/ comments · ⌘/Ctrl+Enter runs · Esc leaves the editor
Step 6
Get it accepted
All test cases pass
Step 7
Review it so it sticks
Re-solve from memory after
The step that matters most: without reviews most of a solved problem fades within weeks. Three short re-solves, each just as you start to forget, are what make it yours.
Your notes
Three lines in your own words. They come back with this problem's reviews, so you can check yourself after recalling the solution.
Solve it yourself before you look
Reading the solution first feels like progress, but it makes the next similar problem — and the interview version — much harder, because you skipped the part where you figure it out. Give it an honest 20–30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.
Hidden: approach · solution code · optimal solution
Approach
Pattern Recognition
Two pointers from both ends — the first character belongs last and the last belongs first, so swap them and walk inward. When the pointers meet, the string is reversed, and you never needed a second array.
Approach
Brute Force — Build a Reversed Copy, O(n)
rev = s[::-1] # a brand-new reversed list
s[:] = rev # copy it back
Works, but it spends O(n) extra memory — exactly what the problem forbids.
Optimized — Swap In Place, O(n)
left, right = 0, len(s) - 1
while left < right:
s[left], s[right] = s[right], s[left]
left += 1
right -= 1
Example: ["h","e","l","l","o"] — swap h/o, then e/l, and the middle l stays put → ["o","l","l","e","h"].
Complexity
| Approach | Time | Space | Notes |
|---|---|---|---|
| Reversed copy | O(n) | O(n) | Extra array, breaks the O(1) requirement |
| Swap in place | O(n) | O(1) | Two pointers, each character moved once |
Solution code
class SolutionBruteForce:
"""O(n) time | O(n) space — build a reversed copy, then copy it back."""
def reverseString(self, s: list[str]) -> None:
rev = s[::-1]
s[:] = rev
class Solution:
"""O(n) time | O(1) space — two pointers swap from both ends."""
def reverseString(self, s: list[str]) -> None:
left, right = 0, len(s) - 1
while left < right:
s[left], s[right] = s[right], s[left]
left += 1
right -= 1Other ways to solve it
- Thought of another approach (slower, or cleverer)? Code it here: the same tests check any approach.
- Community solutions on LeetCode ↗ for many more versions, in many languages. Read them after you've solved it.
Revising this one?
- →Solve it from memory before peeking
- →Say the pattern out loud, not just the code
- →Explain it like you would to a friend
- →Try it again in a different language