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{
return true;}
}

public int getNextNumber(){

Random randomNGen=new Random();
int generatedN=randomNGen.nextInt(1000);
while(IsNumberTaken(generatedN)==true){
generatedN=randomNGen.nextInt(1000);
}
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();

}

}``````

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.

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.

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.