FastPrepLongest Subarray Within an Absolute-Difference Limit
Problems/Longest Subarray Within an Absolute-Difference Limit

Longest Subarray Within an Absolute-Difference Limit

Google logoGoogle● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionExamples

Problem statement

Source note: In a Google onsite coding round, the candidate got a question they describe as close to a well-known array problem: find the longest stretch of consecutive values whose spread stays within a limit. They first planned a two-pointer sliding window, then mixed in a deque and a stack and needed a hint before finishing. A follow-up coding part was also completed but not described.

The report does not say how the asked question differed from the well-known one, and gives no examples or limits. Here we use the standard form of that problem: return the length of the longest run of consecutive elements whose largest minus smallest value is at most the limit. The limits and examples are ours, and the undescribed follow-up is not included.

Our version is about 70% the same as the reported question.

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
Pro

Examples

Example 1

nums = [3,9,5,6,4,10]limit = 3return = 3
  • The subarray [5, 6, 4] has largest value 6 and smallest value 4, so every pair inside it differs by at most 2, which is within the limit 3. Its length is 3.
  • Every longer subarray contains a pair that differs by more than 3: any length-4 subarray contains 9 and 5, or 10 and 5, or 9 and 4.
  • So the answer is 3.
02 · Example 2
FastPrep Pro
Reported in 1 Google interview this week

Unlock 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

Interview reports that mention this question

  • Google Experienced SWE First Round and Onsite: 1 Experience

Google interview experiences

  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
  • Google AI Coding Interview and AI OA: 2 Experiences
  • Google SWE Intern Interview Results Timeline: 13 Experiences
  • Google L4 SWE Phone Screen and Kirkland Onsite: 1 Experience
All Google interview questionsSee Google hiring insights
CodePython 3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import heapq
from collections import defaultdict
 
class Solution:
def solve(self, values, queries):
# Build the adjacency structure
n = len(values)
graph = defaultdict(list)
for u, v in queries:
graph[u].append(v)
 
best = [float("inf")] * n
heap = [(0, 0)]
while heap:
cost, node = heapq.heappop(heap)
if cost > best[node]:
continue
Run and Submit unlock with Pro
FastPrep Pro
Reported in 1 Google interview this week

Unlock 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