FastPrepLargest Number with Adjacent Digits Differing by One
Problem · Breadth First Search

Largest Number with Adjacent Digits Differing by One

Learn this problem
MediumAmazon logoAmazonNEW GRADONSITE INTERVIEW
See Amazon hiring insights

Problem statement

A nonnegative integer is a stepping number when the absolute difference between every pair of adjacent decimal digits is exactly 1. Every one-digit number, including 0, is a stepping number.

Given a nonnegative integer limit, return the largest stepping number that is less than or equal to limit.

Function

largestSteppingNumber(limit: long) → long

Examples

Example 1

limit = 0return = 0

0 is itself a stepping number.

Example 2

limit = 21return = 21

The digits 2 and 1 differ by exactly one, so the limit itself qualifies.

Example 3

limit = 100return = 98

98 qualifies, while no stepping number lies from 99 through 100.

Constraints

  • 0 <= limit <= 10^15.

More Amazon problems

drafts saved locally
public long largestSteppingNumber(long limit) {
  // write your code here
}
limit0
expected0
checking account