Waiter

Sort by

recency

|

66 Discussions

|

  • + 0 comments

    poorly written problem

  • + 0 comments

    Very confusing question with very poor explanation of what it's asking you to do. Essentially, what it wants you to do is do q iterations where each iteration corresponds to a prime starting from 2 (iteration 0=2, iteration 1=3, iteration 2=5, etc), and see if that given prime is a factor of a value in the array. If the prime is a factor move the value to an answer array, if not move the number to some new array. Remember this question is about a STACK of plates, if the prime is a factor of a value that values plate stays in place, but if not it gets moved to a seperate stack.

    example: input [4, 3, 2, 7, 11], doing one iteration so the prime is 2 4 is at the bottom of the stack of plates, 11 is the top. 2 is a factor of both 4 & 2, so those plates stay in that stack [4, 2], but for 3, 7, & 11, 11 is now at the bottom. [11, 7, 3]. After doing all the iterations(in this example we're doing 1 to reiterate) the question says store the remaining plates top to bottom, so the final answer stack becomes [4, 2, 3, 7, 11]

    If we were to do the same example again but with two iterations: On iteration 0 we end up same as before with two stacks ans: [4, 2], remaining: [11, 7, 3]. Now on iteration 1, 3 is prime factor we are checking for. 3 is a prime factor of 3 so we lift that into ans [4, 2, 3], the remaining stack ends up flipped to [7, 11]. Since we've done our two iterations, we move the remaining numbers to the ans stack, which means flipping them again so the final ans stack becomes [4, 2, 3, 11, 7].

    Hope this clarifies a little about what exactly this question wants you to do.

    Heres an inplace C++ solution.

    // precalculate primes up to 10k (1229 primes), max possible input number is 10000, max iteration is 1200
    int primes[] = {2, 3, ..., 9973};
    
    vector<int> waiter(vector<int> number, int q) {
        auto it = number.begin();
        for (int i = 0; i < q; i++) {
            // if it can't be partitioned further
            if (it == number.end()) {
                return number;
            }
            // get prime
            int divisor = primes[i];
            // stable partition to keep relative order, if prime is factor, value moved to left partition
            it = stable_partition(it, number.end(), [&](int x) { return x % divisor == 0; });
            // as the plates are on a stack need to be reversed, "top" is now "bottom"
            reverse(it, number.end());
        }
        // store remaining plates top to bottom which means reversing again.
        reverse(it, number.end());
        return number;
    }
    
  • + 0 comments

    This is truly an exercise in clarifying requirements and a bit of computer science trivia. Great for competitive coders, questionable for interview prep. Not impossible that someone will interview with this problem but very rarely would this be an appropriate measure of problem solving and job fit.

    Go ahead and try it to familiarize with the problem but if you get stuck, just start reading instead. You'll learn more than by thinking in circles.

    I also really appreciate the amount of thoughtful responses here instead of mindlessly posting solutions!!! Way to go HackerRank community!!!

  • + 0 comments

    ths one is all over the place. It mixes metaphors; goes back and forth between stating the problem and instructing how to implement solution. Are talkng stacks of plates? Why is it telling me to create arrays?

    You know, problems are supposed to be difficult to solve. Not difficult to understand what meant to say the AI that wrote it.

  • + 1 comment

    Typescript

    function isPrime(num:number){
        if(num<2)return false;
        for(let i=2; i<=Math.sqrt(num); i++){
            if(num%i===0){
                return false;
            }
        }
            return true;
    }
    

    function waiter(number: number[], q: number): number[] {

    const primeArr = (()=>{
        let primes : number[] = [];
        let num = 2;
        while(primes.length < q){
            if(isPrime(num)){
                primes.push(num);
            }
            num++;
        }
        return primes;
    })()
    
    let rem :number[] = number;
    let ans : number[] = [];
    
    for(let i=0; i<q; i++){
    
             let temp =[];
            for(let k=0; k<rem.length; k++){
                if(rem[k]%primeArr[i]===0){
                    ans.push(rem[k]);
                }
                else {
                    temp.unshift(rem[k])
                }
            }
            rem = [...temp];
    
    }
    for(let i=rem.length-1; i>=0; i--){
        ans.push(rem[i]);
    }
    return ans;
    

    }