Longest Subarray Within an Absolute-Difference Limit
Updated
Problem statement
You are given an integer array nums and an integer limit.
A subarray is a non-empty run of consecutive elements of nums. A subarray is allowed when every two of its elements differ by at most limit, that is, when its largest value minus its smallest value is at most limit.
The problem statement continues
ProExamples
Example 1
nums = [3,9,5,6,4,10]limit = 3return = 3- The subarray
[5, 6, 4]has largest value6and smallest value4, so every pair inside it differs by at most2, which is within the limit3. Its length is3. - Every longer subarray contains a pair that differs by more than
3: any length-4 subarray contains9and5, or10and5, or9and4. - So the answer is
3.
FastPrep Pro
Reported in 1 Google interview this weekUnlock this recently reported problem
FastPrep Pro gives you full access to interview problems reported within the last week.
- Full problem statement and constraints
- 2 more worked examples, explained
- Guided hints and editorial
- Run your code on real test cases
$8.25/month
$99 billed yearly — or $19 month-to-month. Cancel anytime.
Free plan — 2 of 2 free unlocks used this week
More Google Onsite Interview questions
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
- Course Schedule Cycle DetectionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Filesystem Entity Total SizeONSITE INTERVIEW · Seen Nov 2026
- Reach a Destination Through Scheduled FlightsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Russian Doll EnvelopesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Longest Continuous Subarray Within an Absolute-Difference LimitUber · ONSITE INTERVIEWUber interview questions
- Sliding-Window Rate LimiterSnowflake · ONSITE INTERVIEWSnowflake interview questions
- Sliding Window MaximumAmazon · ONSITE INTERVIEW
- Fixed-Window Rolling MeanJump Trading · ONSITE INTERVIEWJump Trading interview questions
- Sliding Window MaximumPeopleStrong · ONSITE INTERVIEWPeopleStrong interview questions
- Sliding Window MaximumBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Sliding Window Average (Meta Canada, E4/E5 :)Meta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Disk Space Window AnalyzerAgoda · PHONE SCREENAgoda interview questions
- Longest Continuous Subarray Within an Absolute-Difference LimitOracle · PHONE SCREENOracle interview questions
- Minimum of Fixed-Window MaximaJuspay · OA