KatInterview

Exact Staffing for a 2:1 Nail-to-Screw Quota

Math & Bit ManipulationAuto-graded · 24 test cases
EasyAuto-gradedMath & Bit Manipulationmathratiosdivisibility

Last updated

Compute the headcount of two specialist worker types so a component quota splits exactly 2:1 with every worker at full capacity, or report that no such staffing exists.

Staffing a Fastener Line

A workshop assembles products from two kinds of fasteners, nails and screws. They must be used in an exact 2 : 1 ratio: the number of nails is exactly twice the number of screws.

Each day the workshop has a target total_components, the number of nails plus the number of screws. Two kinds of specialists do the installing:

  • A nail installer places exactly 500 nails per day and no screws.
  • A screw installer places exactly 250 screws per day and no nails.

Every installer works a full day at exactly the stated rate. There are no partial days and no partially used installers. No other worker types exist.

Task

Given the integer total_components, find a number of nail installers a and a number of screw installers b (both integers) such that:

  1. nails = 500 * a and screws = 250 * b.
  2. nails + screws == total_components.
  3. nails == 2 * screws.

Output

  • If such a and b exist, return the list [a, b, a + b]. The values are unique, because the two equations fix nails and screws.
  • If no such integers exist, return None. Never round, approximate, or choose a "closest" staffing.
  • If total_components is 0, return [0, 0, 0].
  • The rule is purely arithmetic and applies to any integer input. A negative quota is not special-cased: a and b are still the integers solving the equations above, so they may be negative. For example, -750 gives [-1, -1, -2], and a negative value that has no integer solution gives None.

Constraints

  • total_components is an integer. Do not assume it is positive or small, and use exact integer arithmetic (no floating point).
  • The return value is either a list of three integers or None.

Examples

Example 1

Input: exact_staffing(10000)

Output: None

Example 2

Input: exact_staffing(300)

Output: None

Example 3

Input: exact_staffing(750)

Output: [1, 1, 2]

Example 4

Input: exact_staffing(0)

Output: [0, 0, 0]

Hints

Hint 1

If nails are twice the screws, every 3 fasteners in the quota split into 2 nails and 1 screw. What does that say about divisibility of the quota by 3?

Hint 2

Once you know the exact nail and screw counts, a staffing exists only if the nail count is a multiple of 500 and the screw count is a multiple of 250. Handle the ratio first, then convert to workers.

Hint 3

Write the quota as 3m, so nails = 2m and screws = m. Work out what both divisibility conditions imply for the quota; you should find it must be a multiple of 750, and then both worker counts are equal.