Goldman Sachs Interview Question for SDETs


Country: India
Interview Type: Phone Interview




Comment hidden because of low score. Click to expand.
0
of 0 vote

Java provides library to create random number generator and generate random numbers. I have used system current time in milliseconds as seed so that even when JVM is re-booted there is very high chance that numbers will not be repeated. If one has to be hundred percent sure , then we will need to write all the random numbers to a file before JVM is shut down and then compare those while generating random numbers.

import java.util.Random;

public class RandomNumberGenerator {
	
	public static void main(String[] args) throws InterruptedException {
		for (int i = 0; i < 10; i++) {
			System.out.println(getRandom(100));
			Thread.sleep(1);
		}
		
		for (int i = 0; i < 10; i++) {
			System.out.println(getRandom());
			Thread.sleep(1);
		}

	}
	
	static int getRandom(int range){
		Random rand = new Random(System.currentTimeMillis());
		return rand.nextInt(range);
	}
	
	static int getRandom(){
		Random rand = new Random(System.currentTimeMillis());
		return rand.nextInt();
	}

}

- praveenkcs28 October 27, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

The above code doesn't handle the uniqueness.

public static void main(String...args){
        List<Integer> numbers = new ArrayList<Integer>();
        Test test = new Test();
        for(int i=0; i< 5; i++){
           int random = test.generateRandom(5);
           while(numbers.contains(random)){
               random = test.generateRandom(5);
           }
           numbers.add(random);
        }
        
        for(int i = 0; i< numbers.size(); i++){
            System.out.println(numbers.get(i));
        }
    }

    private int generateRandom(int range){
        Random random = new Random(System.currentTimeMillis());
        return random.nextInt(range);
    }

- Sat October 27, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

It is not necessary to use new Random(System.currentTimeMillis()) here, because new Random() does the same thing.

public Random() {
    this(System.currentTimeMillis());
}

/**
 * Creates a new pseudorandom number generator, starting with the
 * specified seed, using <code>setSeed(seed);</code>.
 *
 * @param seed the initial seed
 */
public Random(long seed) {
    setSeed(seed);
}

- mons November 09, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

If java.util.Random is allowed to use, we could implement it like this:

/**
 * Get random number without range.
 * 
 * @return random number
 */
int getRandom() {
	return new Random().nextInt();
}

/**
 * Get random number with range.
 * 
 * @param min
 * @param max
 * @return random number
 */
int getRandom(int min, int max) {
	return new Random().nextInt((max - min) + 1) + min;
}

- mons November 09, 2014 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More