Count and Say

MediumStringssimulation

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

The count-and-say sequence is a sequence of digit strings. Term 1 is "1". Each later term describes the previous term aloud: split it into maximal runs of the same digit, and for each run write the run length followed by the digit. For example, "3322251" has runs 33, 222, 5, 1, so it is described as "23" + "32" + "15" + "11" = "23321511". Given a positive integer n, return the n-th term of the sequence.

Examples

Input: n = 1
Output: "1"
Term 1 is defined as "1".
Input: n = 4
Output: "1211"
"1" -> "11" -> "21" -> "1211" (one 2, one 1).
Input: n = 5
Output: "111221"
"1211" has runs 1, 2, 11, described as "11" + "12" + "21".

Constraints

  • 1 <= n <= 30

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More simulation problems