Magicsheet logo

Reformat Phone Number

Easy
100%
Updated 6/1/2025

Asked by 1 Company

Topics

Reformat Phone Number

What is this problem about?

The Reformat Phone Number problem asks you to normalize a phone number by removing spaces and dashes, then regrouping digits into blocks of 3, except when 1 or 2 digits remain (form a group of 2+2 or just 2). This easy coding problem tests string chunking logic. The string interview pattern is demonstrated.

Why is this asked in interviews?

Activision asks this to verify careful string chunking with a special edge case for the final grouping. It's a practical implementation problem testing attention to the "2-digit leftover" rule.

Algorithmic pattern used

Clean + chunk + special last group. Remove all spaces and dashes. While length > 4: take 3 digits. For remaining 4: split as 2+2. For remaining 3: take as one group. For remaining 2: take as one group. Join with '-'.

Example explanation

s="1-23-45 6". Clean: "123456". Length=6.

  • Take "123". Remaining "456" (3 digits): take as one group. Result: "123-456".

s="123 4-567 89 10". Clean: "1234567890". Length=10.

  • "123","456","78","90"? No: 10 chars: take "123","456", remaining 4: "78","90". Result: "123-456-78-90".

Common mistakes candidates make

  • Not handling the 4-remaining case (must split 2+2, not 3+1 or 4).
  • Not stripping dashes along with spaces.
  • Off-by-one in the remaining digits condition.
  • Taking groups from the wrong end.

Interview preparation tip

String chunking problems with special tail handling require processing the tail differently from the body. Always handle the tail cases last: if remaining=4 → 2+2; if remaining=3 → 3; if remaining=2 → 2. Process the main body in fixed blocks first. Practice similar "formatted output with special tail" problems: "split string into k-char groups," "format credit card number," "break text into lines."

Similar Questions