Two Sigma Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

As far as I understanded from the explanation, I write following in java.

import java.util.List;
import java.util.ArrayList;
import java.util.Random;


public class PhoneNumbers {
	
	public static List<Integer> takenNumbers=new ArrayList<Integer>();
	
	public boolean IsNumberTaken(int num){
		
		return takenNumbers.contains(num);
	}
	
	public boolean giveOut(int num){
		
		if(IsNumberTaken(num)){
			
			return false;
		}
		else{ 
			takenNumbers.add(num);
			return true;}
	}
	
	public int getNextNumber(){
		
		Random randomNGen=new Random();
		int generatedN=randomNGen.nextInt(1000);
		while(IsNumberTaken(generatedN)==true){
			generatedN=randomNGen.nextInt(1000);
		}
		takenNumbers.add(generatedN);
		return generatedN;
	}
	
	public void listTakenNumbers(){
		
		System.out.println("Printing taken numbers ");
		for(int i: takenNumbers){
			System.out.println(i);
		}
		
	}
	
	public static void main(String[] args){
		
		PhoneNumbers PN=new PhoneNumbers();
		PN.giveOut(123);
		PN.giveOut(125);
		PN.listTakenNumbers();
		System.out.println("is 123 taken " +PN.IsNumberTaken(123));
		System.out.println("The next number given: " + PN.getNextNumber());
		PN.listTakenNumbers();
		
		
	}

}

- Anonymous October 17, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Why not just have a counter C that ranges from 000-000-0000 through 999-999-9999. GIVE_ME_ONE will give out C and increment C by one. The other two functions can just compare n to C.

- Anonymous October 20, 2017 | 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