Waiter

Sort by

recency

|

64 Discussions

|

  • + 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;
    

    }

    • + 0 comments
      1. First understand what are factors, e.g., 4 and 6 are factors of 24.
      2. Secondly, understand that prime numbers only have 2 factors : 1 and itself. And 24 has 1,2,3,4,6,8,12,24 as factors. Hence it's not a prime number.
      3. Thirdly, understand that to identify whether a number is a prime number from a programming stand point, we need to loop from 1 to 24 to see how many factors we are getting. This way is called brute force, but it takes a very long time.
      4. If we take a closer look, we notice a pattern, 2 * 12 is 24, 3 * 8 is 24, 4 * 6 is 24, and vice versa, 6 * 4 is 24, 8 * 3 is 24, 12 * 2 is 24. And square root of 24 is between these two sets of factors. 1, 2, 3, 4, 4.8989, 6, 8, 12, 24
      5. So instead of looping all the way to 24, we can just loop up to square root of 24
  • + 0 comments

    Java Solution:-

    public static List<Integer> waiter(List<Integer> number, int q) {
            List<Integer> results = new ArrayList<>();
            int max = number.stream().max( Integer::compareTo ).orElse(2) ;
            List<Integer> primes = sieveOfAtkin( max ) ;
            Stack<Integer> ai = new Stack<>();
            Stack<Integer> bi = new Stack<>();
            for(int k=0; k<q; ++k ) {
                if (k<primes.size()) {
                    Integer prime = primes.get(k);
                    System.out.println("prime: "+prime) ;
                    while(!number.isEmpty()) {
                        Integer value = number.remove(number.size()-1);
                        if (value%prime == 0)
                            bi.push(value);
                        else
                            ai.push( value );
                    }
                    stackToList(bi,results );
                    number.addAll(ai) ;
                    ai.clear();
                } else {
                    if ( (q-k)/2 == 0 )
                        Collections.reverse(number);
                    break;
                }
            }
            Collections.reverse(number);
            results.addAll(number);
            
            return results;
        }
        private static void stackToList(Stack<Integer> stack, List<Integer> list) {
            while(!stack.isEmpty()) {
                list.add(stack.pop()) ;
            }
        }
        private static List<Integer> sieveOfAtkin( final int limit ) {
            List<Integer> primes = new ArrayList<>();
            boolean[] sieve = new boolean[limit+1] ;
            if ( sieve.length > 2 )
                sieve[2] = true;
            if ( sieve.length > 3 )
                sieve[3] = true;
            for ( int x=1; x*x <= limit; ++x ) {
                for ( int y=1; y*y <=limit; ++y ) {
                    int xx = x*x;
                    int yy = y*y;
                    int n = (4*xx)+yy;
                    if (n<=limit && ( n % 12==1 || n % 12==5 ))
                        sieve[n] ^= true;
                    n = (3*xx)+yy;
                    if (n<=limit && ( n % 12==7 ))
                        sieve[n] ^= true;
                    n = (3*xx)-yy ;
                    if ( x > y && n <= limit && n % 12 == 11 )
                        sieve[n] ^= true;
                }
            }
            for ( int r = 5; r*r <=limit; ++r ) {
                if (sieve[r]) {
                    for ( int i = r*r; i<= limit; i += r*r ) {
                        sieve[i] = false;
                    }
                }
            }
            
            for ( int i = 2; i <= limit; ++i ) {
                if (sieve[i])
                    primes.add(i) ;
            }
            
            return primes;
        }
    
        }
    
  • + 0 comments

    This is silly.