Smallest Lexicographic Label With a Given Length and Letter Sum

Last updated: November 29, 2026

Quick Overview

Letters score a=1 through z=26. Given a required label length n and a target total k, return the lexicographically smallest lowercase string of length n whose letter scores sum to k.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

0

0 solved


Letters score a=1 through z=26. Given a required label length n and a target total k, return the lexicographically smallest lowercase string of length n whose letter scores sum to k.

A quick greedy used in Google phone screens to check you can reason about which end of a string to optimize. The interviewer expects you to fill the back with 'z' as much as possible so the front can stay 'a'.

What the Interviewer Expects
  • Notice that early positions matter most for lexicographic order
  • Keep front letters as small as possible
  • Fill from the end with the largest letter allowed
  • Leave at least 1 per remaining position
  • O(n) time
Key Topics to Cover
Greedy
Strings
Lexicographic order
Math
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
  • Return the lexicographically largest such string.
  • Some letters are banned.
  • Add a constraint of no two equal adjacent letters.
  • Validate inputs where no solution exists.
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: ints n and k with n <= k <= 26n. Output: smallest string of length n with letter sum k....

High-Level Design

Lexicographic order is decided by the first differing position, so we want as many leading a letters as possible and the smallest letter at the first non-a position. That means pushing the excess ...


Submit Your Answer
Markdown supported