Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Whiteboard tests and similar

Options
  • 11-10-2013 4:02pm
    #1
    Registered Users Posts: 18


    Hey guys, just looking to get some feedback on the following little tests, I want to start practicing these for the inevitable whiteboard tests in interviews, I have solutions for these two already, but they're not exactly fancy, and I have no idea how to properly measure my solutions either. Any gurus want to throw in their 2 cents? I'm working in java btw.
    Your program is to use the brute-force approach in order to find the Answer to Life, the Universe, and Everything. More precisely... rewrite small numbers from input to output. Stop processing input after reading in the number 42. All numbers at input are integers of one or two digits.
    
    
    Example
    Input:
    1
    2
    88
    42
    99
    
    Output:
    1
    2
    88
    
    You are asked to calculate factorials of some small positive integers.
    
    Input
    
    An integer t, 1<=t<=100, denoting the number of testcases, followed by t lines, each containing a single integer n, 1<=n<=100.
    
    Output
    
    For each integer n given at input, display a line with the value of n! ("n factorial").
    
    Example
    
    Sample input:
    4
    1
    2
    5
    3
    Sample output:
    
    1
    2
    120
    6
    


Comments

  • Registered Users Posts: 26,558 ✭✭✭✭Creamy Goodness


    throw up your attempts. how are we to know you're just not cribbing our answers.

    we'll pick it apart and tell you what we'd do differently/change.

    the best part of this approach, we all learn.


  • Registered Users Posts: 18 Zema Grim


    Of course, sorry. Nothing fancy going on, which is why I'm sure there's better solutions. The second one was annoying merely for having to use BigInteger, I wasn't aware of it prior to attempting that problem, and I feel like if I had been asked how to handle the huge numbers from factorials I would have been a bit stumped!
    import java.util.*;
    import java.lang.*;
    
    class Main
    {
    	public static void main (String[] args) throws java.lang.Exception
    	{
    	Scanner scn = new Scanner(System.in);
            
            int input=0;
            while(true){
                input = scn.nextInt();
                if(input==42){
                    break;
                }
                System.out.println(input);
            }
    	}
    }
    
    import java.util.*;
    import java.lang.*;
    import java.math.BigInteger;
    
    class Main
    {
        public static void main (String[] args) throws java.lang.Exception
    	{
    		Scanner scn = new Scanner(System.in);
           
            int inputs = scn.nextInt();
            int[] arr = new int[inputs];
            
            for(int i = 0; i < inputs; i++){
                arr[i] = scn.nextInt();
            }
            
            for(int j = 0; j < arr.length; j++){
                
                int num = arr[j];
                BigInteger fact = BigInteger.valueOf(1);
                for(int i = num; i > 0; i--){
                    fact = fact.multiply(BigInteger.valueOf(i));
                }
                
                System.out.println(fact);
            }
    	}
    }
    


  • Registered Users Posts: 1,922 ✭✭✭fergalr


    Solutions look fine to me at a glance, for whatever that's worth. (looking at the overview algorithm, not checking for syntax etc.)


    I can't think of anything complicated or clever in the first one.


    For the second one, you did it iteratively rather than recursively, which I suppose is good, no stack issues (although the biggest allowed N should be fine recursively anyway).


    I guess that there'll be a lot of wasted computation in the second solution, calculating the factorial separately for each test case. You could just calculate all factorials N!, for 0<N<=100 in one go, and then look them up for each test case.


    But that probably wouldn't make a difference if the algorithms are being assessed electronically in a screening process (which is what those questions are designed for, (think I saw them on hacker news in the last few days?))


    If you were getting whiteboard questions, you'd generally instead be given questions where there were a few different ways of improving the solution so that people could see how far you get.


    Separately, there's often better solutions for these simple problems if you go into the maths of it. (Better ways of calculating factorial).
    E.g. if someone gives you fibonacci to do on a whiteboard, you could always write down the closed form solution.
    If you've wasted your time memorising it...

    But interviewers shouldn't going to expect someone in a general software engineering role to have a big store of number theory results off the top of their head, and shouldn't be looking for that.


    That's all the feedback I can think of...


Advertisement