FastPrepJob Runner Chain Reports

Job Runner Chain Reports

Headlands Technologies logoHeadlands Technologies● HardFULLTIMENEW GRADOA
Learn

Problem statement

You receive a CSV-like job log in lines. The first line must be exactly #job_id,runtime_in_seconds,next_job_id. Every later line must contain exactly three comma-separated decimal integers in that order.

Each job has a positive unique job_id, a nonnegative runtime in seconds, and a next_job_id. A next ID of 0 ends a chain. A valid log forms a disjoint union of one or more simple singly linked chains: every nonzero successor exists, no job has more than one predecessor, every chain has exactly one head, and following successors eventually reaches 0.

If any syntax or structural rule is violated, return the one-element array ["Malformed Input"].

Otherwise, return one report string for each chain, ordered by descending total runtime. If two chains have equal total runtime, order those chains by ascending starting job ID. Format each report exactly as:

start_job=S,last_job=L,number_of_jobs=N,job_chain_runtime=HH:MM:SS,average_job_time=HH:MM:SS

The total runtime is the sum of all runtimes in the chain. The average runtime uses integer division toward zero. Each time field uses two digits for minutes and seconds and at least two digits for hours.

Numeric fields contain only ASCII digits 0 through 9; each is nonempty and within the stated bounds. Leading zeros are allowed, and uniqueness is checked on the numeric job ID. A log containing only the header is malformed because a valid log contains at least one job. Hours do not wrap at 24.

Function

summarizeJobChains(lines: String[]) → String[]

Examples

Example 1

lines = ["#job_id,runtime_in_seconds,next_job_id","1,30,2","2,90,0","5,10,6","6,20,7","7,30,0"]return = ["start_job=1,last_job=2,number_of_jobs=2,job_chain_runtime=00:02:00,average_job_time=00:01:00","start_job=5,last_job=7,number_of_jobs=3,job_chain_runtime=00:01:00,average_job_time=00:00:20"]

The chain 1 -> 2 totals 120 seconds, and 5 -> 6 -> 7 totals 60 seconds. Descending total runtime puts the 120-second chain first.

Example 2

lines = ["#job_id,runtime_in_seconds,next_job_id","10,5,30","20,7,30","30,11,0"]return = ["Malformed Input"]

Job 30 has two predecessors, so the rows do not form disjoint singly linked chains.

Example 3

lines = ["#job_id,runtime_in_seconds,next_job_id","9,3661,0"]return = ["start_job=9,last_job=9,number_of_jobs=1,job_chain_runtime=01:01:01,average_job_time=01:01:01"]

A single terminal job is a valid one-job chain. Its total and average runtimes are equal.

Example 4

lines = ["#job_id,runtime_in_seconds,next_job_id","1,60,23","2,23,3","3,12,0","23,30,0"]return = ["start_job=1,last_job=23,number_of_jobs=2,job_chain_runtime=00:01:30,average_job_time=00:00:45","start_job=2,last_job=3,number_of_jobs=2,job_chain_runtime=00:00:35,average_job_time=00:00:17"]

The original sample has totals 90 and35 seconds; the second average is floor(35/2) = 17 seconds.

Example 5

lines = ["garbage"]return = ["Malformed Input"]

The complete source malformed example becomes a one-line array and returns the exact error.

Example 6

lines = ["#job_id,runtime_in_seconds,next_job_id","1,4,0","90,15,91","91,15,0","7,20,0"]return = ["start_job=90,last_job=91,number_of_jobs=2,job_chain_runtime=00:00:30,average_job_time=00:00:15","start_job=7,last_job=7,number_of_jobs=1,job_chain_runtime=00:00:20,average_job_time=00:00:20","start_job=1,last_job=1,number_of_jobs=1,job_chain_runtime=00:00:04,average_job_time=00:00:04"]

Totals30,20,4 must be ordered by runtime, not ascending start ID or input order.

Constraints

  • 1 <= lines.length <= 100001.
  • Each line has length at most 120.
  • Valid numeric fields are decimal digit sequences with no sign, spaces, or extra characters.
  • For valid logs, 1 <= job_id <= 10^9, 0 <= runtime_in_seconds <= 10^9, and 0 <= next_job_id <= 10^9.
  • Every valid chain total fits in a signed 64-bit integer.

More Headlands Technologies problems

See Headlands Technologies hiring insights
public String[] summarizeJobChains(String[] lines) {
    // write your code here
}
lines["#job_id,runtime_in_seconds,next_job_id","1,30,2","2,90,0","5,10,6","6,20,7","7,30,0"]
expected["start_job=1,last_job=2,number_of_jobs=2,job_chain_runtime=00:02:00,average_job_time=00:01:00", "start_job=5,last_job=7,number_of_jobs=3,job_chain_runtime=00:01:00,average_job_time=00:00:20"]
Checking account…