Largest Number Below N from Reusable Digits
Updated
Problem statement
Given a positive integer n and a set of decimal digits digits, construct the largest non-negative integer that is strictly smaller than n and uses only digits from digits.
Each allowed digit may be reused any number of times. A multi-digit result cannot begin with zero, but the one-digit result 0 is valid. Return -1 when no valid result exists.
Function
largestNumberBelow(n: int, digits: int[]) → intExamples
Example 1
n = 23415digits = [2,4,9]return = 22999The prefix 23 cannot be continued with an allowed digit below 4, so the construction backtracks to 22 and fills the suffix with 9.
Example 2
n = 100digits = [0,1]return = 11No valid three-digit answer is below 100, so the best two-digit construction is 11.
Constraints
1 <= n <= 10^91 <= digits.length <= 100 <= digits[i] <= 9- All values in
digitsare distinct.
More ByteDance Phone Screen questions
- Count N-Queens PlacementsPHONE SCREEN · Seen Nov 2026
- Matrix EscapePHONE SCREEN · Seen Nov 2026
- Log Analyzer: Busiest Service in a Rolling WindowPHONE SCREEN · Seen Nov 2026
- Majority ElementPHONE SCREEN · Seen Nov 2026
- Valid ParenthesesPHONE SCREEN · Seen Nov 2026
- Binary Tree Target-Sum PathsPHONE SCREEN · Seen Nov 2026
- Longest Increasing SubsequencePHONE SCREEN · Seen Nov 2026
- Alien DictionaryPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Lexicographically Smallest Topological OrderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Maximum Number of Events That Can Be AttendedPHONE SCREEN · Seen Nov 2026
Similar questions at other companies
- Maximize Workday EarningsCitadel · OA · PHONE SCREEN · ONSITE INTERVIEWCitadel interview questions
- Rearrange Digits Under an Adjacent-Sum LimitAmazon · PHONE SCREEN
- Biggest Number From Gluing Numbers TogetherAdobe · PHONE SCREENAdobe interview questions
- Most Relevant Text SpanHebbia · PHONE SCREEN
- Text JustificationUber · PHONE SCREENUber interview questions
- Find GPU Capacity and Drain a NodeTogether AI · PHONE SCREEN
- Word Break IIBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Wizard Scroll Allocation StrategyJane Street · ONSITE INTERVIEWJane Street interview questions
- Insert Addition and Multiplication OperatorsPinterest · ONSITE INTERVIEWPinterest interview questions
- Maximum Unique-Character Word SubsetMeta · ONSITE INTERVIEWMeta interview questions
ByteDance interview experiences
- ByteDance TikTok USDS SWE Intern Interview: 1 Experience
- ByteDance MLE New Grad Interviews: 1 Experience
- ByteDance Security SRE Intern Phone Screens: 1 Experience
- ByteDance USDS New Grad SWE Phone Screen: 1 Experience
- ByteDance New Grad Data Engineer Video Interviews: 1 Experience
- ByteDance Trust & Safety Research Scientist Intern: 1 Experience