Expedia Interview Question for Software Developers


Country: United States




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

You guys missed the most efficient solution. If the remainder units (k%N) is more than than (N+1)/2, Harry can choose to hop down 1 step of time to a position which is exactly N units, then hop up N units. That is more efficient than directly hop up 1 step at a time.

For example, N is 7 and there are 5 units left. The naive way is one unit up at a time and need 5 more steps. Since 5 is more than (7+1)/2 = 4, the more efficient way is hop down 2 units, make the remaining units 7, then hop up 7 units, total remaining steps is 3.

The answer is (k/n) + (((k%n)>((k+1)/2))? (n+1 - (k%n)) : (k%n))

- Anthony Mai February 23, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

if(k > n):
return (k/n) + ( k%n > n/2 ? (n - (k%n) + 1) : (k%n) );

if(k == n):
return 1;

if(k < n):
return k;

Simple O(1) solution :)

EDIT: edited based on observation by Anthony Mai's answer.

- Killedsteel February 23, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public int minimumHops(int k, int n) {
return (k/n + k%n);
}

For example : k = 68 and n = 10
Answer = 6 (N hop) + 8 (1 hop) = 14hops
k/n = 6 + k%n = 8 = 14

- Naveen Shan February 23, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public int minimumHops(int k, int n) {
	return (k/n + k%n);
}

Example : k = 68 and n = 10
Answer : 6 (n hops : 60) + 8 (1 hops : 8) = 14hops
k/n (68/10 = 6) + k%n (68%10 = 8) = 14hops

- Naveen Shan February 24, 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