## EMC Interview Question Software Engineer / Developers

• 0

Given a sorted array with repeated nos e.g

1,2,2,7,7,7,7,9,9...

Find first occurence of any given value.

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

Given: array is of integers.

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

This has been already discussed , we can do it with the help of Hash Map...with a complexity of o(1)

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

Hi,
can you please share link to the solution. I could not figure out o(1) solution. Are you sure about that? Isnt it o(log n)?

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

move in the step of power of ...
2,4,8, 16, 32,.. --> 2^i
moment u find a number less then or equal the required number stop.
if(num <= arr[2^i])
break
Apply binary search between the current index 2^i and 2^(i-1)

solution O(log n)

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

but i think he wants the first occurrence of the number.
so i think we need to apply binary search to find the number and then move left or right to find the first occurrence.

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

moron piece of shit solution

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

yeah ...the above solution is much better than hashing as hashing takes a preprocessing time of O(n) and space complexity of O(n) .

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

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

We can directly apply binary search on this array because it is sorted.
Suppose an sorted array A[1..n].
In every step of binary search, we don't return if A[mid]==key.
Instead, we use this conditional statement:
if (key<=A[mid]) high=mid-1
else low=mid+1.
At last, we return value of low when low>high;
Time complexity is O(logN);

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book walking you through every aspect of getting a job at a top tech company, while focuses on software engineering interviews.

### 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.