Compute reachability and minimal-time same-type scheduling
Company: Amazon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Take-home Project
Quick Answer: This prompt evaluates integer reachability and invariant reasoning via repeated additive transformations (testing number-theoretic reachability and state-space analysis) alongside resource-aware scheduling and grouping logic for minimizing completion time under type and memory constraints, and it falls under the Coding & Algorithms domain.
Reachability under (a,b)->(a+b,b) / (a,b)->(a,a+b)
Constraints
- 1 <= a, b, c, d <= 1000
- Operations may be applied any number of times and in any order
- Values only ever increase, so reachability is checked by reducing (c, d) back toward (a, b)
Examples
Input: (1, 1, 5, 2)
Expected Output: True
Explanation: (1,1)->(1,2)->(3,2)->(5,2).
Input: (1, 1, 4, 7)
Expected Output: True
Explanation: Reachable by a sequence of additions (reverse-reduces (4,7) back to (1,1)).
Input: (2, 3, 2, 3)
Expected Output: True
Explanation: Already equal; zero operations needed.
Input: (5, 2, 1, 1)
Expected Output: False
Explanation: Target is smaller than the start; values can only grow.
Input: (1, 1, 2, 2)
Expected Output: False
Explanation: From (1,1) you can reach (1,2) or (2,1) but never (2,2).
Input: (3, 5, 8, 5)
Expected Output: True
Explanation: (3,5)->(8,5) directly via (a+b,b).
Input: (1, 1, 1000, 1)
Expected Output: True
Explanation: Repeatedly apply (a+b,b) keeping b=1 to grow a up to 1000.
Input: (4, 6, 5, 9)
Expected Output: False
Explanation: Reverse reduction lands on (1,0), not (4,6).
Hints
- Both operations strictly increase one coordinate, so (c, d) must be component-wise >= (a, b) to be reachable.
- Work backwards from (c, d): the larger coordinate must have come from repeatedly adding the smaller one, so reduce it modulo the smaller (like the Euclidean algorithm) instead of subtracting one step at a time.
- Stop reducing a coordinate once it reaches its target (a or b): then check whether the remaining difference in the other coordinate is an exact multiple of the fixed one.
Minimum time to process same-type tasks in pairs
Constraints
- taskMemory and taskType have the same length n
- All memories and types are positive integers
- Two tasks may share a time slot only if same type and combined memory <= maxMemory
- Each task takes exactly 1 unit of time
Examples
Input: ([7, 2, 3, 9], [1, 2, 1, 3], 10)
Expected Output: 3
Explanation: Type1 {7,3} pair (=10), type2 {2} alone, type3 {9} alone -> 3 units.
Input: ([], [], 10)
Expected Output: 0
Explanation: No tasks.
Input: ([5], [1], 10)
Expected Output: 1
Explanation: Single task runs alone.
Input: ([6, 6, 6], [1, 1, 1], 10)
Expected Output: 3
Explanation: No two 6's fit together (6+6=12>10), so each runs alone.
Input: ([1, 1, 1, 1], [1, 1, 1, 1], 10)
Expected Output: 2
Explanation: Pair them into two slots of (1,1).
Input: ([4, 4, 4, 4], [1, 2, 1, 2], 8)
Expected Output: 2
Explanation: Type1 {4,4} pair, type2 {4,4} pair -> 2 units.
Input: ([10, 1, 1, 1], [1, 1, 1, 1], 10)
Expected Output: 3
Explanation: 10 runs alone; the three 1's give one pair plus one solo -> 1+2 = 3.
Input: ([3, 3, 3, 3, 3], [1, 1, 1, 1, 1], 6)
Expected Output: 3
Explanation: Two pairs (3+3=6) plus one leftover -> 3 units.
Hints
- Tasks of different types can never share a slot, so handle each type independently and sum the results.
- Within one type, sort the memories. The classic greedy is two pointers: try to pair the smallest remaining task with the largest remaining task.
- If the smallest + largest exceeds maxMemory, the largest cannot pair with anything (everything else is <= the smallest), so it must run alone; otherwise pair them and move both pointers inward.