Amazon VO Interview Question: Oncall Shifts and Cartesian Product Combinations

14 Views
No Comments

Oncall shifts of people with start and end time

Name | Start | End
Adam | 10 | 100
Ben | 50 | 70
Cathy | 150 | 300
Diane | 60 | 120

We give a start and end time; we want to know who are oncall during that time period.

Output:

Start | End | Names
10 | 50 | Adam
50 | 60 | Adam, Ben
60 | 70 | Adam, Ben, Diane
70 | 100 | Adam, Diane
100 | 120 | Diane
150 | 300 | Cathy

Write a function that, given a jagged matrix as input, generates all combinations containing 1 element from each inner list in the input matrix.

(i.e. Python’s itertools.product)

For example:

input = [["A", "B", "C"],
  ["X", "Y"],
  ["1", "2"],
]

Result:

[
  "A-X-1",
  "A-X-2",
  "A-Y-1",
  "A-Y-2",
  "B-X-1",
  "B-X-2",
  "B-Y-1",
  "B-Y-2",
]

Another example with token order [2, 1, 0]:

1-X-A
2-X-A
1-Y-A
2-Y-A
1-X-B
2-X-B
1-Y-B
2-Y-B

This problem combines two common interview patterns. The oncall portion is a sweep-line/interval aggregation task: collect all start and end boundaries, sort them, and determine which names are active in each time segment. The matrix portion asks for a Cartesian product over a jagged array, generating every combination by taking one element from each inner list in order. Together, they test sorting, boundary handling, and recursive or iterative backtracking for product generation.

END
 0