Yahoo Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

variation of binary search i.e.. reverse binary search.
check the target number is present in below index ranges i.e.. 2^n index range
0-1
2-3
4-7
8-15
16-31
32-63
.
.

if target number is found in any of the above range then apply binary search on data in that range

int start=0, end = 1;
int i = 1;    // to apply binary search from intermediate data
if target <= a[end]
   apply binary search for the range start and end
else
   i *= 2;
   start += i;
   end += i;
   if arr[start] == '$'
      print(data not found)
      break
   else if arr[end] == '$' // start is number and end is '$' then apply BS from start
       i = 1
       end = start + 1

time complexity is O(log n)

- algos July 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I'm not sure that your solution is O(log n) in the worst case, and it is not guaranteed to return the right index. The example case would be {1, 99, $, $, $, $, $, $, .....}, where n = 2 and the first n cells contains integers in sorted order.

- Anthony Mays July 28, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

it's O(log n) only. what is not guaranteed in your example?

{1, 99, $, $, $, $, $, $, .....}

1. k = 1 then in first if condition it finds
2. k = 99 then in first if condition it finds
3. 1<k>99 then in first if condition it returns -1 as number not found
4. k > 99 here first if condition fails so in else start becomes 2, arr[2] = '$' so it returns -1 as number not found

- algos July 28, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Ah, I see where you're going, I misinterpreted the number ranges. Clever solution, you got my vote.

- Anthony Mays July 29, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think a small correction in your code is necessary.

else
   // i *= 2;
   start += i;
   i *= 2;
   end += i;

- riddimon February 02, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The following is a solution in Java. Assumes that sorted integers are unique. We start by looking at position k since we're guaranteed that the value won't be in a position greater than k. Once we've checked k, we can then do a binary search on the range 0..k-1. This solution is O(log k).

public static int findPosition(String[] input, int k) {
	if (input == null || input.length == 0 || input[0] == "$")
		return -1;
	if (input[k] == Integer.toString(k))
		return k;
	return binarySearch(input, k, 0, k-1);
}

public static int binarySearch(String[] input, int k, int start, int end) {
	if (end < start)
		return -1;
	int mid = (start + end) / 2;
	// Convert string array value to int, if possible
	int midValue = -1;
	if (input[mid] != "$")
		midValue = Integer.parseInt(input[mid]);
	// Check mid point
	if (midValue == k)
		return mid;
	// Search low if mid value is greater than k
	// or if it is "$"
	if (midValue > k || input[mid] == "$")
		return binarySearch(input, k, start, mid - 1);
	// If mid point isn't "$", then search the upper range
	else if (input[mid] != "$")
		return binarySearch(input, k, mid + 1, end);
	return -1;
}

- Anthony Mays July 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

But isn't log(k) less than log(n)?

- Anonymous March 28, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You definitely are not supposed to assume that the sorted integers are unique. It sort of takes the challenge out of the problem.

- alex May 06, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Since the target is an infinite array, we are guaranteed not to reach its end (presumably)! We can start by searching every 2^k, k=0,1,... (i.e., arr[(1<<k)-1]) location (hence, o(lg n)) until we
1.either hit the value we are looking for so return the (1<<K)-1 as its location
2. we hit a value greater than the value we are looking for OR a '$'

If the second case occurs, then we know our value should be somewhere between indices 0 and (1<<k)-1. Do a binary search within this limit until you either find the value or figure out no such value exists.

- Anonymous July 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Simply find the value "n" in O(logn) time and then apply binary search.
The value "n" can be found out as follows:
i=1;
while(arr[i]!='$')
i*=2;
n=i;

- Sumit August 04, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool searchInfiniteArray(int[] a)
{
bool lastBlock = false;
for (int k=0; ; k++);
{
int lo(1 << k - 1), hi(1 << (k+1) - 1), x;
while (lo <= hi)
{
x = lo + (hi-lo)/2;
try {
if (a[x] == target) return x;
else if (a[x] > target) return hi = x-1;
else lo = x+1;
}
catch(Exception e) { hi = x-1; lastBlock = true; }
}
if (lastBlock) return false;
}
}

- Anonymous August 04, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

First question to interviewer should be, how can arrays contain combination of int and Strin/Char values? Arrays are strongly typed.

- Anonymous July 31, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

Assuming all integers are unique... we have to find a

right = -1;
loop:
Let left = right + 1;
step: Let diff = a - arr[left];
if diff < 0
    	if left == 0
		a is not in the array;
    	else
		a will be into 0 to left - 1; use binarysearch(arr[], 0, left - 1, a);
else
right = left + diff;
goto loop:

- coding.arya July 27, 2013 | 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