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
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Online Assessment
Coding & Algorithms
Easy

0

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
string formatting
string parsing
modulo arithmetic
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample Answer
Requirements
  • Input: string s of letters, digits, -, and an integer k >= 1.
  • Output: uppercase, groups of k separated by -, first group of length 1 to k.
  • 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...


Submit Your Answer
Markdown supported