FastPrepWorker Management, Part 2: Top Workers

Worker Management, Part 2: Top Workers

Ramp logoRamp● MediumNEW GRADINTERNOA
Learn

Problem statement

The source assessment is a cumulative four-level worker-hours register for contract workers at a facility. Each level includes every operation from the earlier levels. This page implements Levels 1 through 2 and returns one string result for every query in input order.

Level 1: Workers and Office Sessions

  • ["ADD_WORKER", workerId, position, compensation] adds a new worker and stores the worker's position and compensation. If workerId already exists, leave the system unchanged and return false; otherwise return true. Worker IDs and positions contain only English letters and spaces.
  • ["REGISTER", workerId, timestamp] records an office entry when the worker is outside and records an exit when the worker is inside. Calls to REGISTER arrive in increasing timestamp order. Return invalid_request for an unknown worker and registered after a successful entry or exit.
  • ["GET", workerId] returns the worker's total time in the office using completed sessions only. A current session without an exit contributes nothing. Return the empty string when the worker does not exist.

Level 2: Ordered Worker Statistics

  • ["TOP_N_WORKERS", n, position] returns at most n workers whose current position equals position.
  • Order workers by completed office time in descending order. Break equal-time ties by worker ID in ascending alphabetical order.
  • Format the result as workerId1(time1), workerId2(time2), .... Return every matching worker when fewer than n exist, and return the empty string when there are no matches.
  • A worker with no completed office session has time 0.

FastPrep Runner Interface

The source uses an array of query rows and one string output per query. FastPrep exposes the same operation rows as workerManagementLevel2(String[][] operations) and returns String[]. Numeric arguments are encoded as base-10 strings.

Multipart Series

Function

workerManagementLevel2(operations: String[][]) → String[]

Examples

Example 1

operations = [["ADD_WORKER","John","Junior Developer","120"],["ADD_WORKER","Jason","Junior Developer","120"],["ADD_WORKER","Ashley","Junior Developer","120"],["REGISTER","John","100"],["REGISTER","John","150"],["REGISTER","Jason","200"],["REGISTER","Jason","250"],["REGISTER","Jason","275"],["TOP_N_WORKERS","5","Junior Developer"],["TOP_N_WORKERS","1","Junior Developer"],["REGISTER","Ashley","400"],["REGISTER","Ashley","500"],["REGISTER","Jason","575"],["TOP_N_WORKERS","3","Junior Developer"],["TOP_N_WORKERS","3","Middle Developer"]]return = ["true","true","true","registered","registered","registered","registered","registered","Jason(50), John(50), Ashley(0)","Jason(50)","registered","registered","registered","Jason(350), Ashley(100), John(50)",""]

Jason and John initially tie at 50, so Jason comes first alphabetically. Jason's session from 275 to 575 later raises his total to 350.

Constraints

  • 1 <= operations.length <= 500, matching the source's query-count bound.
  • Every row uses an operation available at this level, has the documented arity, and satisfies the source-stated operation preconditions.
  • Every numeric argument is a valid base-10 integer string.
  • Worker IDs and positions contain only English letters and spaces.
  • All REGISTER calls are supplied in increasing timestamp order.

More Ramp problems

See Ramp hiring insights
public String[] workerManagementLevel2(String[][] operations) {
  // write your code here
}
operations[["ADD_WORKER","John","Junior Developer","120"],["ADD_WORKER","Jason","Junior Developer","120"],["ADD_WORKER","Ashley","Junior Developer","120"],["REGISTER","John","100"],["REGISTER","John","150"],["REGISTER","Jason","200"],["REGISTER","Jason","250"],["REGISTER","Jason","275"],["TOP_N_WORKERS","5","Junior Developer"],["TOP_N_WORKERS","1","Junior Developer"],["REGISTER","Ashley","400"],["REGISTER","Ashley","500"],["REGISTER","Jason","575"],["TOP_N_WORKERS","3","Junior Developer"],["TOP_N_WORKERS","3","Middle Developer"]]
expected["true", "true", "true", "registered", "registered", "registered", "registered", "registered", "Jason(50), John(50), Ashley(0)", "Jason(50)", "registered", "registered", "registered", "Jason(350), Ashley(100), John(50)", ""]
Checking account…