Visa OA Coding Interview: Single Machine Queue

18 Views
No Comments

You are a senior chemist working in a cutting-edge molecular research laboratory.

Your team has developed a revolutionary molecular reactor that can synthesize complex compounds through a series of controlled chemical reactions.

The reactor operates on a strict schedule where each synthesis reaction requires exactly 5 minutes to complete.

The laboratory receives research samples at various times throughout the day, and each sample must undergo a synthesis reaction in the molecular reactor.

However, there's a critical limitation: the reactor can only process one sample at a time and samples waiting to be processed are stored in a specialized cooling chamber.

If the cooling chamber contains more than 10 samples, any newly arriving samples will be rejected due to stability concerns.

Your task is to determine the total time required to process all accepted samples through the molecular reactor.

The output should be the time in seconds since the start of the laboratory's operating day.

Notes:

  • The cooling chamber capacity is calculated by the number of samples waiting to start synthesis, not including the sample currently being processed in the reactor.
  • If a new sample arrives at the same moment as when another sample completes its synthesis, the first sample waiting in the cooling chamber starts synthesis first.

Example

For times = [1, 6, 9, 502], the output should be 1201.

For times = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], the output should be 3601.

Input/Output

  • [execution time limit] 0.5 seconds (cpp)
  • [memory limit] 1 GB
  • [input] array.integer times

An array of distinct integers representing arrival times (seconds since the start of the laboratory's operating day). It is guaranteed that all the arrival times are given in increasing order.

Guaranteed constraints:

  • times.length is at least 1
  • The reactor processes one sample at a time
  • The reactor takes 300 seconds to process each sample

This Visa OA problem is a classic single-server simulation with a bounded waiting queue. The clean approach is to process events in time order, using a FIFO queue for samples waiting in the cooling chamber and a current-time pointer for the reactor. When the reactor is free, the next waiting sample starts immediately and occupies 300 seconds. The main edge cases are the capacity rule, which counts only waiting samples, and tie handling when an arrival happens exactly as a synthesis finishes. A straightforward queue-based simulation is enough to pass within the stated time limit.

END
 0