Widest Gap Between a Low Day and a Later Higher Day

Last updated: November 29, 2026

Quick Overview

Given daily retention percentages, return the maximum j - i such that i < j and retention[i] <= retention[j], or 0 if no such pair exists.

Meta
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Given daily retention percentages, return the maximum j - i such that i < j and retention[i] <= retention[j], or 0 if no such pair exists.

A Meta technical-screen problem phrased around retention curves: how far apart can a low day and a later day that matched or beat it be. The interviewer wants you to see that only prefix minima can be the best left endpoint and to use a decreasing stack plus a right-to-left sweep.

What the Interviewer Expects
  • Show the O(n^2) baseline and the O(n log n) sort-by-value idea
  • Argue only strictly decreasing prefix minima can be optimal left endpoints
  • Build that candidate stack in one left pass
  • Sweep j from the right, popping candidates as they are matched
  • Explain why popping is safe
Key Topics to Cover
Monotonic stack
Two pointers
Prefix minimum
Arrays
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Return the pair of days as well as the width.
  • Require retention[j] to be at least retention[i] + d.
  • How would you answer this for many sliding windows of data?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: list r of integers (length up to 5 * 10^4). Output: the largest j - i with i < j and r[i] <= r[j]. Return 0 if the list is strictly decreasing....

High-Level Design

If i is not a new prefix minimum, some earlier i' has r[i'] <= r[i], so i' pairs with every j that i could and gives a wider gap. So the only useful left endpoints are indices where the value strict...


Submit Your Answer
Markdown supported