Waiter HackerRank Solution

In this Waiter HackerRank solution, You are a waiter at a party. There is a pile of numbered plates. Create an empty  array. At each iteration, , remove each plate from the top of the stack in order. Determine if the number on the plate is evenly divisible by the  prime number. If it is, stack it in pile . Otherwise, stack it in stack . Store the values in  from top to bottom in . In the next iteration, do the same with the values in stack . Once the required number of iterations is complete, store the remaining values in  in , again from top to bottom. Return the  array.

Example

An abbreviated list of primes is . Stack the plates in reverse order.

Begin iterations. On the first iteration, check if items are divisible by .

Move  elements to .

On the second iteration, test if  elements are divisible by .

Move  elmements to .

And on the third iteration, test if  elements are divisible by .

Move  elmements to .

All iterations are complete, so move the remaining elements in , from top to bottom, to .

. Return this list.

Function Description

Complete the waiter function in the editor below.

waiter has the following parameters:

  • int number[n]: the numbers on the plates
  • int q: the number of iterations

Returns

  • int[n]: the numbers on the plates after processing

Input Format

The first line contains two space separated integers,  and .
The next line contains  space separated integers representing the initial pile of plates, i.e., .

Constraints



Sample Input

5 1
3 4 7 6 5

Sample Output

4
6
3
7
5

Explanation

Initially:

 = [3, 4, 7, 6, 5]<-TOP

After 1 iteration:

 = []<-TOP

 = [6, 4]<-TOP

 = [5, 7, 3]<-TOP

We should output numbers in  first from top to bottom, and then output numbers in  from top to bottom.

Waiter HackerRank solution

I will Provide solution in Multiple programming languages for you. If you are not able to find the code in required language then please share in comments so that our team can help you.

Problem Solution in JavaScript

function waiter(numbers , q){
    const prime = generatePrimes(q);
    let a = [numbers];
    let b = [[]];
    let answers = [];
    for(let i = 1; i<= q; i++){
        a.push([]);
        b.push([]);
        while(a[i-1].length > 0){
            let num = a[i-1].pop();
            if(num % prime[i-1] == 0)
                b[i].push(num);
            else a[i].push(num);
        }
    }


   b.forEach(x => answers.push(...reverese(x)));
   a.forEach(x => answers.push(...reverese(x)));
   return answers;
}


function reverese(arr){
    let temp = [];
    while(arr.length > 0)
        temp.push(arr.pop());
    return temp;
}


//function checks if the number is prime
function isPrime(n){  5
    // if the number is divisible by i, then n is not a prime number;
    for(let i = 2; i< n-1; i++)
        if(n%i == 0) return false;
    //othervise n is prime
    return true;
}


function generatePrimes(n){
    let primes = [];
    let index = 2;
    while(primes.length < n ){
        if(isPrime(index))
            primes.push(index);
           
        index++;
    }
    return primes;
}

Problem Solution in Python

def nextPrime(primes):
    np = primes[-1]+1
    found=0
    while not found:
        found=1
        for p in primes:
            if np%p==0:
                np+=1
                found=0
                break
    return np    


def waiter(number, q):
    stackB=[]
    ans=[]
    primes=[2]
    for i in range(q):
        stackA = []
        while number:
            if number[-1]%primes[-1]==0:
                stackB.append(number.pop(-1))
            else:
                stackA.append(number.pop(-1))
        while stackB:
            ans.append(stackB.pop(-1))
        number = stackA
        primes.append(nextPrime(primes))
   
    while stackA:
        ans.append(stackA.pop(-1))
       
    return ans

Problem Solution in Java

public static List<Integer> waiter(List<Integer> number, int q) {
        List<Integer> primes = generatePrimes(q);
        List<Integer> answers = new ArrayList<>();
       
        Stack<Integer> answer = new Stack<>();
        Stack<Integer> numbers = makeStack(number);
        Stack<Integer> supported = new Stack<>();
        Stack<Integer> temp = null;
       
        for (Integer p : primes) {
            while(!numbers.empty()){
                if(numbers.peek() % p == 0){
                    answer.push(numbers.pop());
                }else{
                    supported.push(numbers.pop());
                }
            }
            while(!answer.empty()){
                answers.add(answer.pop());
            }
            temp = numbers;
            numbers = supported;
            supported = temp;
        }
        while(!numbers.empty()){
            answers.add(numbers.pop());
        }
        return answers;
    }
   
    public static Stack<Integer> makeStack(List<Integer> number){
        Stack<Integer> stack = new Stack<>();
        for (Integer n : number) {
            stack.push(n);
        }
        return stack;
    }
   
   
   
    public static List<Integer> generatePrimes(int n){
        List<Integer> primes = new ArrayList<>();
        int count = 0;
        boolean isPrime = true;
        int index = 0;
        for(int i = 2; i < Integer.MAX_VALUE; i++){
            if(count == n){
                break;
            }
            index = i / 2;
            while(index > 1){
                if(i % index == 0){
                    isPrime = false;
                    break;
                }
                index--;
            }
            if(isPrime){
                primes.add(i);
                count++;
            }
            isPrime = true;
        }


        return primes;
    }
Solve original Problem on HackerRank here. Checkout more HackerRank Problems

Leave a Comment