Reformat Phone Number
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 neededProblem
You are given a phone number string number made of digits, spaces, and dashes. First remove every space and dash. Then split the remaining digits, left to right, into blocks:
- While more than 4 digits remain, take a block of 3.
- When exactly 4 digits remain, split them into two blocks of 2.
- When 2 or 3 digits remain, they form one final block.
Return the blocks joined with single dashes. No block ever has length 1.
Examples
Constraints
- 2 <= number.length <= 100
- `number` contains only digits, spaces, and dashes
- `number` contains at least 2 digits
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.