Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

1) Iterate through array and check if the element is equals to 'e'.
Time Complexity : O(n)
Space Complexity: O(1)
2) Sorting the array (eg. quick sort, merge or heap based on the sorted elements).
Use binary search to find first and last location of 'e'
Sorting Time Complexity: O(n log n).
Searching Time Complexity: log n

- Srigopal Chitrapu December 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 4 vote

1) Just iterate over array and check if the element is equal 'e'. O(n) time O(1) space
2) If you sort an array (eg. quick sort) you can use binary search to find first and last location of 'e'.
Whole operation - O(n logn) time, O(1) space , Bin Search O(log(n)) time
So 2 is better only if we are considering searching phase. For whole algo better is 1.

- thelineofcode December 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Binary search is O(log(n)), unless you are considering the complexity of sorting as well. The question is asking "After sorting" what is the complexity.

- Ehsan December 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 votes

Yep, taking into account only binary search it is - O(logn).

- thelineofcode December 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sorting the array can be O(n) by using

int counter[255];

for all characters.
Then re-scanning 'e' would be O(1) by summarizing all counters before 'e'.

- DoZerg December 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think, the question should show, if you know about radix-sort algorithm. In this case, the range is small and known in advance (a-Z),, so radix sort can be used and time complexity of "sorting" is 0(n)...

- tpcz December 21, 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