Google · Question 18 · Strings
Count and Say
Problem
Given an integer n, generate the nth term of the count-and-say sequence.
Approach
Iteratively build the string by counting consecutive characters.
Time O(n*2^n)Space O(2^n)
How it works
- 1Start with base '1'.
- 2For each iteration, count consecutive chars and form the new string.
- 3Use nested loops to process the current string.
- 4Return the result after n iterations.
Sample input / output
Input: 4 Output: 1211
Constraints
1 <= n <= 30.
Solution — read it, then walk it
Time O(n*2^n)Space O(2^n)Loops 3Branches 1Returns 1source · Python
Scroll sideways for wide charts — the view follows the active step.
- start / end
- statement
- condition
- loop
- returns
- definition
1/23
StartSTART
Interactive control-flow diagram and step-by-step code walkthrough.
Recommended resourcesAffiliate links · may earn us a commission
Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits