Most Ad Revenue From a Region by Day Rectangle Under a Cap

Last updated: November 29, 2026

Quick Overview

Given a grid of daily revenue deltas (rows are regions in a fixed order, columns are days), find the largest sum of any contiguous rectangle that does not exceed a cap k.

Meta
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


Given a grid of daily revenue deltas (rows are regions in a fixed order, columns are days), find the largest sum of any contiguous rectangle that does not exceed a cap k.

A hard Meta onsite question that layers an ordered-set trick on top of the classic 2D to 1D reduction, framed around ads revenue planning. Interviewers look for fixing a pair of rows, collapsing to column sums, and then using sorted prefix sums with a ceiling lookup instead of Kadane, which cannot handle the cap.

What the Interviewer Expects
  • Explain why Kadane does not work with an upper bound
  • Reduce 2D to 1D by fixing top and bottom rows
  • Use sorted prefix sums with a ceiling search for p >= running - k
  • Iterate the smaller dimension in the outer loops
  • State the complexity with a balanced BST versus a Python list
Key Topics to Cover
ordered set
prefix sums
binary search
2D to 1D reduction
matrix
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
  • What if the cap is a lower bound instead (largest sum at least k)?
  • What if all values are non-negative: can you use two pointers?
  • How would you parallelize over row pairs?
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: m by n integer grid (values may be negative), cap k.
  • Output: the maximum rectangle sum that is <= k, or None if every rectangle exceeds k.
  • Rectangles are contiguous in both dimensions...
High-Level Design

Step 1, reduce to 1D. Fix a top row and extend the bottom row downward, maintaining col_sums[c] as the sum of column c between them. Every rectangle corresponds to one (top, bottom) pair and one...


Submit Your Answer
Markdown supported