6 kyu

The Supermarket Queue

5,833 of 55,054mattlub

Description:

There is a queue for the self-checkout tills at the supermarket. Your task is write a function to calculate the total time required for all the customers to check out!

input

  • customers: an array of positive integers representing the queue. Each integer represents a customer, and its value is the amount of time they require to check out.
  • n: a positive integer, the number of checkout tills.

output

The function should return an integer, the total time required.


Important

Please look at the examples and clarifications below, to ensure you understand the task correctly :)


Examples

queueTime([5,3,4], 1)
// should return 12
// because when there is 1 till, the total time is just the sum of the times

queueTime([10,2,3,3], 2)
// should return 10
// because here n=2 and the 2nd, 3rd, and 4th people in the 
// queue finish before the 1st person has finished.

queueTime([2,3,10], 2)
// should return 12
queueTime [5,3,4] 1
-- should return 12
-- because when there is 1 till, the total time is just the sum of the times

queueTime [10,2,3,3] 2
-- should return 10
-- because here n=2 and the 2nd, 3rd, and 4th people in the 
-- queue finish before the 1st person has finished.

queueTime [2,3,10] 2
-- should return 12
queue_time([5,3,4], 1)
# should return 12
# because when n=1, the total time is just the sum of the times

queue_time([10,2,3,3], 2)
# should return 10
# because here n=2 and the 2nd, 3rd, and 4th people in the 
# queue finish before the 1st person has finished.

queue_time([2,3,10], 2)
# should return 12
queue_time([5,3,4], 1)
# should return 12
# because when n=1, the total time is just the sum of the times

queue_time([10,2,3,3], 2)
# should return 10
# because here n=2 and the 2nd, 3rd, and 4th people in the 
# queue finish before the 1st person has finished.

queue_time([2,3,10], 2)
# should return 12
queueTime(std::vector<int>{5,3,4}, 1)
// should return 12
// because when n=1, the total time is just the sum of the times

queueTime(std::vector<int>{10,2,3,3}, 2)
// should return 10
// because here n=2 and the 2nd, 3rd, and 4th people in the 
// queue finish before the 1st person has finished.

queueTime(std::vector<int>{2,3,10}, 2)
// should return 12
queueTime [5;3;4] 1
// should return 12
// because when there is 1 till, the total time is just the sum of the times

queueTime [10;2;3;3] 2
// should return 10
// because here n=2 and the 2nd, 3rd, and 4th people in the 
// queue finish before the 1st person has finished.

queueTime [2;3;10] 2
// should return 12
int customers1[] = {5, 3, 4};
int customers1_length = 3;
int n1 = 1;
queueTime(customers1, customers1_length, n1)
// should return 12
// because when n=1, the total time is just the sum of the times

int customers2[] = {10, 2, 3, 3};
int customers2_length = 4;
int n2 = 2;
queueTime(customers2, customers2_length, n2)
// should return 10
// because here n=2 and the 2nd, 3rd, and 4th people in the 
// queue finish before the 1st person has finished.

int customers3[] = {2, 3, 10};
int customers3_length = 3;
int n3 = 2;
queueTime(customers3, customers3_length, n3)
// should return 12
      QueueTime [5,3,4] 1 => 12
      * because when there is 1 till, the total time is just the sum of the times

      QueueTime [10,2,3,3] 2 => 10
      *  because here n=2 and the 2nd, 3rd, and 4th people in the 
      * queue finish before the 1st person has finished.

      QueueTime [2,3,10] 2 => 12

Clarifications

  • There is only ONE queue serving many tills, and
  • The order of the queue NEVER changes, and
  • The front person in the queue (i.e. the first element in the array/list) proceeds to a till as soon as it becomes free.

N.B. You should assume that all the test input will be valid, as specified above.

P.S. The situation in this kata can be likened to the more-computer-science-related idea of a thread pool, with relation to running multiple processes at the same time: https://en.wikipedia.org/wiki/Thread_pool

Arrays
Fundamentals

More By Author:

Check out these other kata created by mattlub

Stats:

CreatedAug 14, 2016
PublishedAug 14, 2016
Warriors Trained159411
Total Skips36849
Total Code Submissions304688
Total Times Completed55054
JavaScript Completions17467
Python Completions21876
Ruby Completions1569
C++ Completions3596
C# Completions3292
Java Completions5833
Haskell Completions293
F# Completions61
C Completions832
COBOL Completions9
Julia Completions23
Rust Completions458
D Completions7
Go Completions530
Total Stars5014
% of votes with a positive feedback rating93% of 5889
Total "Very Satisfied" Votes5214
Total "Somewhat Satisfied" Votes577
Total "Not Satisfied" Votes98
Total Rank Assessments10
Average Assessed Rank
6 kyu
Highest Assessed Rank
5 kyu
Lowest Assessed Rank
7 kyu
Ad
Contributors
  • mattlub Avatar
  • jhoffner Avatar
  • GiacomoSorbi Avatar
  • siebenschlaefer Avatar
  • Dentzil Avatar
  • JohanWiltink Avatar
  • user4912975 Avatar
  • mmalkavian Avatar
  • Madjosz Avatar
  • neilm Avatar
  • hobovsky Avatar
  • stellartux Avatar
  • user8436785 Avatar
  • ejini战神 Avatar
  • akar-0 Avatar
  • dfhwze Avatar
Ad