FastPrepRectangle Point Coverage Counts

Rectangle Point Coverage Counts

OpenAI logoOpenAI● MediumFULLTIMEPHONE SCREEN

Problem statement

You are given a grid of cells that is width cells wide and height cells tall. Cell (x, y) is in column x and row y, where 0 <= x < width and 0 <= y < height. At the start, the grid holds no rectangles.

Process the strings in operations in order. Each one is one of these:

  • "ADD id x1 y1 x2 y2": add a rectangle named id. It covers every cell (x, y) with x1 <= x <= x2 and y1 <= y <= y2, so its corner and edge cells are covered too.
  • "REMOVE id": remove the rectangle named id. It no longer covers any cell.
  • "COUNT_AT x y": count how many rectangles currently in the grid cover cell (x, y).

Rectangles may overlap, and two rectangles may cover exactly the same cells. Each one counts on its own. IDs are case-sensitive, so a and A are different IDs. Once a rectangle is removed, its id is free again, and a later ADD may use it for a new rectangle.

The problem statement continues
Pro

Examples

Example 1

width = 5height = 4operations = ["ADD a 0 0 2 1","ADD b 1 1 4 3","COUNT_AT 1 1","COUNT_AT 2 0","COUNT_AT 4 3","COUNT_AT 0 3","REMOVE a","COUNT_AT 1 1"]return = [2,1,1,0,1]

Rectangle a covers columns 0 to 2 of rows 0 and 1. Rectangle b covers columns 1 to 4 of rows 1 to 3.

  • Cell (1, 1) is inside both rectangles, so the answer is 2.
  • Cell (2, 0) is inside a only, because b starts at row 1. The answer is 1.
  • Cell (4, 3) is the far corner of b. Corner cells are covered, so the answer is 1.
  • Cell (0, 3) is in neither rectangle, so the answer is 0.
  • After REMOVE a, only b covers cell (1, 1), so the answer is 1.
FastPrep Pro
Reported in 1 OpenAI 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
See OpenAI hiring insights
CodePython 3
Run and Submit unlock with Pro
FastPrep Pro
Reported in 1 OpenAI 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