Microsoft Interview Question for Software Engineer / Developers






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

keep inspecting the incoming values. Till you have o or 1 value in the array stay at the first index of the array. When second value comes , if this 2nd value is smaller than the current value present jump to the new value ( assuming array is sort in ascending manner). This way keep inspecting new incoming values, if all values are greater than the value to be found try to point to the 1st value. if all values in array are smaller than the value to be found, stay at the last value.
If some values are lesser than the value to be found and some greater than the value to be found , stay at the largest value smaller than the value to be found

- Anonymous February 12, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

look at 1 2 4 8 16 go on

- Anonymous October 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

is the data being filled in a sorted manner as well ? (if yes, this O(n) soln of going thru the array one-by-one element, either till we find the element or a value greater than it)

do we know the value range of the data in the array?

- morpheus February 12, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think we should use "insertion sort" for inserting elements in array and use Binary Search
Complexity: O(N^2)

- Shwetank March 05, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

1) I think you don't have a choice in sorting it.
2) Binary search needs the length of array.

- peace November 09, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

What is wrong with Binary Search? Check at 1, then 2, then 4, then 8 etc.

- T March 19, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this is not *binary* search you moron

- Anonymous September 24, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think, we can do it in 0(log n) time,

1. take the recent element, which is inserted into arry,
Case 1, recent is greater than the search-key.
Apply Binary search on the existing array.

Case 2, recent is less than the search key,
then wait, upto the arraival of search key through stream.

- Chatrapathy March 22, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is a O(n) solution. If at input you have just 2 elements, say 0,1 and the given value is 1000. So you wait for the incoming values. Assume n-2 values come thru. So you have to compare the value 1000 with n-2 values --> O(n) algorithm

- abhimanipal May 25, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think, we can do it in 0(log n) time,

1. take the recent element, which is inserted into arry,
Case 1, recent is greater than the search-key.
Apply Binary search on the existing array. --time comp: 0(log m), m==> number of elements in array.

Case 2, recent is less than the search key,
then wait, upto the arraival of search key through
stream. --its time complexity: 0(k) ===> search key found after k elements.

Time complexity; 0(log m + k)
please correct me...If any thing wrong :P

- Chatrapathy March 22, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I am not sure if you have access to the internals of the sorted, unbounded array. All you have is a way to get the ith element. Something like this:

// Third Party Class.
// You don't have access to internals.
class UnBoundedSortedArray {
    // No idea of implementation.
}

// Your Method.
void SearchForElement(UnBoundedSortedArray array, Element x) {
    
    // All you can do is
    // Element tmp = array[i];
    
    // Implement this method to check if Element x 
    // Is part of the array.
    
}

- T March 22, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Chatrapathy's solution is a good solution.

Another way to handle is to fix the size of "k" and do Binary Search on elements in (n, n+k), (n+k+1, n+2k) until X is found

- Nachiketha April 07, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Another way to handle this question would be to do some basic reading.

- Zing! April 07, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

how can you say that the order of insertion is also increasin or decreasing.what is mentioned is that the elements in array are in sorted order...

- parkhi September 01, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

all you guys are just posting shitty solutions. can someone please post a real working solution.

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

There is yet another O(log n) solution.

This is with assumptions that we dont know how many elements are existing in the array currently and hence binary search is ruled out.

start with 1st(2^0) element, compare and proceed with 2^1 element and so on like . When the searched_for element is less than the array[2^n) element, do binary search for limits between 2^(n-1) and 2^n.

Ofcourse the boundary conditions of array count must be compared with 2^n and handled.

- peace November 10, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if the array is sorted & unbounded ( upon adding the new values - if the array is still sorted with out sorting being done on it ) then the first soln provided here is good enough .

- Anonymous November 15, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i think first we have to maintain searching and insertion mutually exclusive (like consumer and produce) when we search element should store in buffer and when search complete we can search ..

suppose at instant no key value not found i have n element and more element can come .
if we enter in search insertion is lock ..

in search
check 2^0 and 2^1 2^2 ... untill either array ends or at 2^i > search key if second one work .. now we know that at this instan search key is in between 2^(i-1) / 2^i now do binary search if element is present then return ;
now open lock ..means now we can insert element ...after this new element came .... again if we enter in search then we have to look only from previous 2^(i-1) because
search key when ever comes will be placed furthur from previous 2^(i-1) in array
keep this continuing untill we find array

- ridercoder October 22, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If the index of the last element that is recently introduced into the array is unkown, then we do not know high and we cannot apply binary search just like that. So two solutions are there depending on what we have at our disposal.

1.Index of last element is known - in other words the size of array is known.

Check if a[lastIndex] > key
a. If so apply binary search b/w 0 to lastIndex-1.

2.Index of the last element is unknown. Then we would have to search at 2, 4, 8, 16 locations and see if we either find an index where the element is found or an index whose value is greater than the element to be found. In the former, we have the index to return. In the latter case, we would have to do binary search between last index which we checked and the current index where the number is higher than the element to be searched.

- pawan.it17 September 28, 2019 | 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