Shortest Path Through Numbers Sharing a Digit
Updated
Problem statement
You are given an array of distinct non-negative integers numbers and two values start and end that appear in the array.
You can move directly between two values when they share at least one digit in their ordinary decimal representations. Find a path from start to end using these moves.
Return a path with the minimum number of edges. Among several shortest paths, return the lexicographically smallest value sequence. Return an empty array when no path exists. The decimal representation of zero contains digit 0.
Function
shortestCommonDigitPath(numbers: int[], start: int, end: int) → int[]Examples
Example 1
numbers = [1,20,40,41]start = 1end = 20return = [1,41,40,20]Each consecutive pair shares digit 1, 4, and 0 respectively.
Example 2
numbers = [12,23,34,45]start = 12end = 45return = [12,23,34,45]The chain shares digits 2, 3, and 4.
Example 3
numbers = [10,22,35]start = 10end = 35return = []No pair in the array shares a digit.
Constraints
1 <= numbers.length <= 200000.0 <= numbers[i] <= 10^9, and all values are distinct.startandendappear innumbers.
More Google Phone Screen questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Periodic-Color Binary Forest RootsPHONE SCREEN · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Best Stock Trade with Buy and Sell DaysPHONE SCREEN · Seen Nov 2026
- Word Chain Jumps in a GridPHONE SCREEN · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Apartment Housemate PreferencesPHONE SCREEN · Seen Nov 2026
- Price at Query TimestampsPHONE SCREEN · Seen Nov 2026
- Course Schedule Cycle DetectionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Linked Merchants Within Two HopsStripe · PHONE SCREENStripe interview questions
- Bus RoutesBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Jump Game IVZomato / Eternal · ONSITE INTERVIEWZomato / Eternal interview questions
- Largest Island After One FlipOracle · ONSITE INTERVIEWOracle interview questions
- Mutual-Friend RecommendationsMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Shortest Pin Path Through TopicsPinterest · PHONE SCREENPinterest interview questions
- Currency Conversion RateAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Route Graph Reachability with a Broken LinkFigma · PHONE SCREENFigma interview questions
- Minimum Currency ConversionsEPAM · PHONE SCREENEPAM interview questions
- Finite Web CrawlerMongoDB · PHONE SCREEN · ONSITE INTERVIEWMongoDB interview questions