PracHub
QuestionsLearningGuidesInterview Prep

Quick Overview

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.

  • medium
  • Amazon
  • Coding & Algorithms
  • Software Engineer

Compute reachability and minimal-time same-type scheduling

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Take-home Project

1) Given four integers a, b, c, d (1 <= a, b, c, d <= 1000), you may perform the following operations any number of times and in any order: (a, b) -> (a + b, b) or (a, b) -> (a, a + b). Return true or false: can (a, b) be converted to (c, d)? 2) Given an array taskMemory of n positive integers representing the memory required for each task, an array taskType of n positive integers representing task types, and an integer maxMemory, each task takes 1 unit of time. The server can process at most two tasks in parallel only if they are the same type and together require no more than maxMemory units. Compute the minimum time required to process all tasks. Example: n = 4, taskMemory = [7, 2, 3, 9], taskType = [1, 2, 1, 3], maxMemory = 10 -> answer = 3 units.

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)

Given four integers a, b, c, d (1 <= a, b, c, d <= 1000), you may apply, any number of times and in any order, either operation: - (a, b) -> (a + b, b) - (a, b) -> (a, a + b) Return true if and only if the pair (a, b) can be transformed into the pair (c, d). Example: (1, 1) can become (5, 2) via (1,1)->(1,2)->(3,2)->(5,2), so the answer is true. (5, 2) cannot become (1, 1) because the values only ever grow, so that answer is false.

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)).

Hints

  1. Both operations strictly increase one coordinate, so (c, d) must be component-wise >= (a, b) to be reachable.
  2. 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.
  3. 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

You are given an array taskMemory of n positive integers (the memory each task needs), an array taskType of n positive integers (each task's type), and an integer maxMemory. Each task takes 1 unit of time. The server can process at most two tasks in parallel, and only if they are the SAME type and their combined memory is at most maxMemory. Otherwise a task is processed alone. Compute the minimum total time to process all tasks. Example: n = 4, taskMemory = [7, 2, 3, 9], taskType = [1, 2, 1, 3], maxMemory = 10. Type 1 has memories {7, 3} which pair (7+3=10 <= 10) into 1 unit; type 2 {2} takes 1 unit; type 3 {9} takes 1 unit. Total = 3 units.

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.

Hints

  1. Tasks of different types can never share a slot, so handle each type independently and sum the results.
  2. Within one type, sort the memories. The classic greedy is two pointers: try to pair the smallest remaining task with the largest remaining task.
  3. 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.
Last updated: Jun 26, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Compute Edit Distance - Amazon (medium)
  • Minimize Replacements So Equal Product Values Are Contiguous - Amazon (hard)
  • Find a Maximum-Sum Window in a Sparse Array - Amazon (hard)
  • Add One Source to Minimize Grid Inconvenience - Amazon (medium)
  • Implement a Half-Open Interval Range Module - Amazon (hard)