We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I guess that this task is designed to help us prepare for the real challenges of engineering work, as it is a great exercise in dealing with confusing customer requirements ;)
What helped me understand the baffling description and solve the problem was following the proposed analogy with plates, and visualizing how they are being transferred from/to different stacks by the waiter (I even drew a couple of diagrams on a sheet of paper).
JavaScript solution:
functionwaiter(number,q){// Write your code hereconstanswers=[];constprimes=[];letnum=2;while(primes.length<q){letisPrime=true;for(letdiv=2;num>div;div++){if(num%div===0){isPrime=false;break;}}if(isPrime)primes.push(num);num++;}functionmovePlatesOneByOne(fromStack,toStack){for(leti=fromStack.length-1;i>=0;i--){toStack.push(fromStack[i]);}fromStack.length=0;}constplates=number.reverse();conststackA=[];// divisible by prime: falseconststackB=[];// divisible by prime: truefor(letprimeofprimes){if(stackA.length){movePlatesOneByOne(stackA,plates);}for(letplateofplates){if(plate%prime===0){stackB.push(plate);}else{stackA.push(plate);}}plates.length=0;if(stackB.length){movePlatesOneByOne(stackB,answers);}}if(stackA.length){movePlatesOneByOne(stackA,answers);}returnanswers;}
`
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Waiter
You are viewing a single comment's thread. Return to all comments →
I guess that this task is designed to help us prepare for the real challenges of engineering work, as it is a great exercise in dealing with confusing customer requirements ;)
What helped me understand the baffling description and solve the problem was following the proposed analogy with plates, and visualizing how they are being transferred from/to different stacks by the waiter (I even drew a couple of diagrams on a sheet of paper).
JavaScript solution:
`