DSA interview question
You're building a toy "data compression" utility for an embedded systems team — the simplest possible run-length encoding demo used in onboarding material for new hires. The classic illustrative example is the count-and-say sequence: each term is generated by "reading off" the previous term, describing consecutive runs of the same digit as (count)(digit). Your task: given n, generate the n-th term of this sequence.
The sequence starts:1 11 (one 1) 21 (two 1s) 1211 (one 2, one 1) 111221 (one 1, one 2, two 1s) 312211 (three 1s, two 2s, one 1)
string countAndSay(int n);
Input n | Output | Explanation |
|---|---|---|
1 | "1" | Base case |
2 | "11" | "one 1" → "11" |
3 | "21" | "two 1s" → "21" |
4 | "1211" | "one 2, one 1" → "1211" |
5 | "111221" | "one 1, one 2, two 1s" → "111221" |
6 | "312211" | "three 1s, two 2s, one 1" → "312211" |
7 | "13112221" | One more iteration applied to "312211" |
n 1-indexed (i.e., n=1 returns "1")? (Standard convention — confirm.)n? (LeetCode constraints: 1 <= n <= 30 — small range, since the string length grows roughly exponentially with n, so don't worry about extreme optimization here, but mention the growth rate awareness.)n <= 30, this won't happen in practice, but worth flagging as an edge case to be aware of.)n?"On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.
Book a free mock interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in