Asked at 6 companies
5 recent
- Bloomberg
- Amazon
- Microsoft
- Meta
- tcs
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
Watch the concept video if the pattern is new to you.
Step 2
Try it on paper
5–10 minutes, no code yet
ProblemProblem statementGiven a string s consisting of words and spaces, return the length of the last word in the string.ReadHide
Problem Statement
Given a string s consisting of words and spaces, return the length of the last word in the string.
A word is a maximal substring consisting of non-space characters only.
""" + PROV + r"""
Input / Output
Input: s = "Hello World"
Output: 5
Explanation: The last word is "World" with length 5.
Input: s = " fly me to the moon "
Output: 4
Explanation: The last word is "moon" with length 4.
Input: s = "luffy is still joyboy"
Output: 6
Explanation: The last word is "joyboy" with length 6.
Code it on LeetCode
There's no editor for this one here yet, so solve it on LeetCode, with its own tests. Then tick My code passed below, and it joins your reviews. With your LeetCode account linked, that happens on its own.
An error or a Wrong Answer there? What each one means.
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
Approach
Pattern Recognition
Scan from the end — you only care about the last word, so there is no reason to read the whole string from the front. Walk backwards past any trailing spaces, then count letters until you hit a space (or the start). The count is the answer, and you never stored a single word.
Approach
Brute Force — Split and Take the Last Word, O(n)
words = s.split() # splits on spaces, drops the empties
return len(words[-1])
Short and correct, but split() builds a whole list of every word just to look at the last one.
Optimized — Walk Backwards, O(n)
i = len(s) - 1
while i >= 0 and s[i] == ' ': i -= 1 # skip trailing spaces
length = 0
while i >= 0 and s[i] != ' ': # count the last word
length += 1
i -= 1
return length
Example: " fly me to the moon " — skip the two trailing spaces, then count m-o-o-n (4) and stop at the space before the.
Complexity
| Approach | Time | Space | Notes |
|---|---|---|---|
| Split the string | O(n) | O(n) | Builds a list of every word |
| Walk backwards | O(n) | O(1) | Reads only the tail of the string |
Solution code
class SolutionBruteForce:
"""O(n) time | O(n) space — split into words and take the last one."""
def lengthOfLastWord(self, s: str) -> int:
return len(s.split()[-1])
class Solution:
"""O(n) time | O(1) space — walk backwards past trailing spaces, then count."""
def lengthOfLastWord(self, s: str) -> int:
i = len(s) - 1
while i >= 0 and s[i] == " ":
i -= 1
length = 0
while i >= 0 and s[i] != " ":
length += 1
i -= 1
return lengthOther ways to solve it
- 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