Regroup a Gift Card Code Into Uppercase Dash-Separated Blocks of K
Last updated: November 29, 2026
Quick Overview
Given a gift card code containing letters, digits, and dashes placed arbitrarily, return it reformatted in uppercase with dashes removed and reinserted so every group has exactly K characters except the first, which may be shorter but not empty.
Meta
November 29, 20260
0
0 solved
Given a gift card code containing letters, digits, and dashes placed arbitrarily, return it reformatted in uppercase with dashes removed and reinserted so every group has exactly K characters except the first, which may be shorter but not empty.
Meta Quest and app store purchases involve redeemable codes typed by users, so normalizing them is a realistic string task. Online assessments use it to check careful handling of the short first group and of inputs that are all dashes.
What the Interviewer Expects
- Strip dashes and uppercase in one pass
- Compute the first group length with modulo
- Build output with a list and join
- Handle an input that is only dashes
- Avoid quadratic string concatenation
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you do it in a single right-to-left pass?
- How would you validate a checksum digit in the code?
- What if the last group, not the first, may be short?
- How would you treat confusable characters like O and 0?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input: string
sof letters, digits,-, and an integerk >= 1. - Output: uppercase, groups of
kseparated by-, first group of length 1 tok. - All-dash input returns
""....
High-Level Design
First collect the characters that are not dashes, uppercased. Let n be that count. The first group has length n % k, or k if that is 0. Emit that group, then every following block of k, joined...