Booking.com Interview Question for Software Developers


Country: United States
Interview Type: Phone Interview




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

Java:

public class IntegerToString {

	public static void main(String[] args) {
		System.out.println(itoa(4973));
		System.out.println(itoa(-59385));
		System.out.println(itoa(0));
	}

	private static String itoa(int num) {
		if(num == 0){
			return "0";
		}
		boolean negative = num < 0;
		String result = "";
		while(num != 0) {
			result = Math.abs(num%10) + result;
			num /= 10;
		}
		return negative ? "-" + result : result;
	}

}

Outputs:

4973
-59385
0

- f.h March 08, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It's actually quite simple. Use mod and div.

- ANOM July 09, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use recursion

char result [50]={'0'};
int i=0;

itoa(int a){
if(!a) return;
else{
itoa(a/10);
result[i++]=a%10+'0';
}
}

Do checks on negative numbers before and increment i by 1 storing '-' at 0th position of result

- Anupam July 10, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use recursion

char result [50];
int i=0;

void itoa(int a){
if(!a) return;
else 
itoa(a/10);
result [i++]= a%10+'0';
}

Check for negative and zero before calling recursion and make changes in the result array accordingly.

- accessdenied July 10, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about this? Nice and slim!

System.out.println("an itoa");

- Anatolii.Stepaniuk June 28, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Solution1 {
	public static void main (String args[]) {
		Solution1 s = new Solution1();
		System.out.println(s.solve(-333));
	}
	public String solve(int number) {
		if (number == 0)
			return "0";
		String results = "";
		boolean negative = false;
		if (number < 0){
			results = "-";
			negative = true;
		}
		if (negative)
			number = number * (-1);
		int remain = number % 10;
		number = number / 10;
		if (number != 0)
			return results+""+solve(number)+remain;
		return results+""+remain;
	}
}

- w.kinaan July 30, 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