Answer Day-and-Hour Rectangle Queries Over a Charge Volume Grid

Last updated: November 29, 2026

Quick Overview

Given a grid of charge counts indexed by day and hour of day, answer many queries for the total over a range of days and a range of hours, where the hour range may wrap past midnight (for example 22:00 to 02:00).

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

0

0

0 solved


Given a grid of charge counts indexed by day and hour of day, answer many queries for the total over a range of days and a range of hours, where the hour range may wrap past midnight (for example 22:00 to 02:00).

The Stripe Dashboard and Sigma let merchants slice payment volume by time, and a precomputed summary turns each query from a scan into a few lookups. The interviewer checks that you build a 2D prefix sum with clean off-by-one handling and decompose the wrap-around case instead of special-casing the math.

What the Interviewer Expects
  • Build an `(R+1) x (C+1)` prefix grid with a zero border to avoid bounds checks
  • Use inclusion-exclusion with four lookups per rectangle
  • Split a wrapping hour range into two non-wrapping rectangles
  • Treat query bounds as inclusive and validate them
  • Contrast precomputation cost with per-query cost
Key Topics to Cover
2D prefix sums
matrix
inclusion-exclusion
range queries
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
  • How would you support point updates as new charges arrive (Fenwick tree in 2D)?
  • How would you handle merchants in different time zones?
  • What if the grid were days by minutes for a year, and memory mattered?
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

grid[d][h] is the count for day d and hour h (24 columns, or any C). Query (d1, d2, h1, h2) is inclusive on both ranges. If h1 > h2, the range wraps: hours h1..C-1 plus 0..h2. Days do ...

High-Level Design

Precompute P[i][j] as the sum of grid[0..i-1][0..j-1]. A rectangle (r1..r2, c1..c2) is P[r2+1][c2+1] - P[r1][c2+1] - P[r2+1][c1] + P[r1][c1]. For a wrapping query, add the rectangle h1..C-1 ...


Submit Your Answer
Markdown supported