Problem · Hash Table

Count Idle Warehouse Robots

Learn this problem
EasyAmazonNEW GRADOA
See Amazon hiring insights

Problem statement

The Amazon warehouse uses autonomous transport units positioned at distinct integer coordinates on a Cartesian plane. A transport unit at (x, y) is considered idle if there is at least one other unit strictly above it on the same vertical line, at least one strictly below it on the same vertical line, at least one strictly to its left on the same horizontal line, and at least one strictly to its right on the same horizontal line.

The neighboring units do not need to be immediately adjacent by distance 1. For example, a unit at (2, 3) has a unit directly above it if another unit exists at (2, y2) for some y2 > 3.

Given arrays x and y describing the coordinates of n transport units, return the number of idle units.

Function

numIdleDrives(x: int[], y: int[]) → int

Examples

Example 1

x = [0, 0, 0, 0, 0, 1, 1, 1, 2, -1, -1, -2, -1]y = [-1, 0, 1, 2, -2, 0, 1, -1, 0, 1, -1, 0, 0]return = 5
Example 1 illustration
The cartesian plane with idle robots marked red can be represented as: The robots at locations (0, 0), (1, 0), (-1, -0), (0, 1), and (0, -1) are idle as they are surrounded by robots on all 4 sides.

Example 2

x = [1, 1, 1, 2, 2, 2, 2, 3, 3, 3]y = [1, 2, 3, 1, 2, 3, 5, 1, 2, 3]return = 2
Example 2 illustration
The 10 robots are arranged as follows:
  • The robot at (2, 2) is idle because it has robots at (1, 2), (3, 2), (2, 3), and (2, 1) directly to the left, right, up, and down respectively.
  • The robot at (2, 3) is idle because it has robots at (1, 3), (3, 3), (2, 5), and (2, 2) directly to the left, right, up, and down respectively.
  • There are 2 idle robots in this arrangement.

    Constraints

    • 1 ≤ n ≤ 105
    • -109 ≤ x[i], y[i] ≤ 109

    More Amazon problems

    drafts saved locally
    public int numIdleDrives(int[] x, int[] y) {
      // write your code here
    }
    
    x[0, 0, 0, 0, 0, 1, 1, 1, 2, -1, -1, -2, -1]
    y[-1, 0, 1, 2, -2, 0, 1, -1, 0, 1, -1, 0, 0]
    expected5
    checking account