Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: Phone Interview
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.
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.
1) Iterate through array and check if the element is equals to 'e'.
- Srigopal Chitrapu December 20, 2013Time 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