Reformat Phone Number

EasyStringssimulation

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

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

Input: number = "1-23-45 6"
Output: "123-456"
Six digits become two blocks of 3.
Input: number = "123 4-567"
Output: "123-45-67"
After one block of 3, the last 4 digits split into 2 and 2.
Input: number = "12"
Output: "12"
Two digits form a single block.

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.

More simulation problems