License Key Formatting

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

A license key s is a string of letters, digits and dashes '-'. The dashes currently split it into arbitrary chunks. Reformat it using a group size k. Discard every dash and convert all letters to **uppercase**. Then split the remaining characters into groups separated by single dashes, where every group has exactly k characters **except the first group**, which may be shorter but must contain at least one character. Group from the right so only the first group can be short. Return the reformatted key. If no letters or digits remain, return an empty string.

Examples

Input: s = "5F3Z-2e-9-w", k = 4
Output: "5F3Z-2E9W"
8 characters remain, forming two groups of 4.
Input: s = "2-5g-3-J", k = 2
Output: "2-5G-3J"
5 characters remain: the first group gets the leftover 1, then groups of 2.
Input: s = "---", k = 3
Output: ""
Nothing but dashes, so the key is empty.

Constraints

  • 1 <= s.length <= 10^5
  • `s` consists of English letters, digits, and dashes `'-'`
  • 1 <= k <= 10^4

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