NVIDIA Interview Question for Software Engineer / Developers






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

Find the maximum repeating number in O(n) time and O(1) extra space

Given an array of size n, the array contains numbers in range from 0 to k-1 where k is a positive integer and k <= n. Find the maximum repeating number in this array. For example, let k be 10 the given array be arr[] = {1, 2, 2, 2, 0, 2, 0, 2, 3, 8, 0, 9, 2, 3}, the maximum repeating number would be 2. Expected time complexity is O(n) and extra space allowed is O(1). Modifications to array are allowed.
Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.

The naive approach is to run two loops, the outer loop picks an element one by one, the inner loop counts number of occurrences of the picked element. Finally return the element with maximum count. Time complexity of this approach is O(n^2).

A better approach is to create a count array of size k and initialize all elements of count[] as 0. Iterate through all elements of input array, and for every element arr[i], increment count[arr[i]]. Finally, iterate through count[] and return the index with maximum value. This approach takes O(n) time, but requires O(k) space.

Following is the O(n) time and O(1) extra space approach. Let us understand the approach with a simple example where arr[] = {2, 3, 3, 5, 3, 4, 1, 7}, k = 8, n = 8 (number of elements in arr[]).

1) Iterate though input array arr[], for every element arr[i], increment arr[arr[i]%k] by k (arr[] becomes {2, 11, 11, 29, 11, 12, 1, 15 })

2) Find the maximum value in the modified array (maximum value is 29). Index of the maximum value is the maximum repeating element (index of 29 is 3).

3) If we want to get the original array back, we can iterate through the array one more time and do arr[i] = arr[i] % k where i varies from 0 to n-1.

How does the above algorithm work? Since we use arr[i]%k as index and add value k at the index arr[i]%k, the index which is equal to maximum repeating element will have the maximum value in the end. Note that k is added maximum number of times at the index equal to maximum repeating element and all array elements are smaller than k.

Following is C++ implementation of the above algorithm.

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

Hash table wont be optimized by space... I think sorting is the technique, if we look for space optimization, and Hash table is the solution if time complexity is more improtant..

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

if we are known the range in which numbers in a given array exists. then we can simply make the bucket for each no .. initially making the bucket value of each no is -1 taking a var Max_repetition =0 and we will traverse the whole array if any number is encounter corresponding number bucket is incremented . and each time of updation of bucket we will check the updated value to the max_repetition value and update accordingly

if range is not known . then we can sort d array in nlogn time and find max repetition.

if any bdy knows more optimal solution please add to it

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

your 1st sol doesnot seem to optimize space.
second is better in terms of space but O(n log n) seems to very large.

- newbeginning September 02, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

a hash map can be used to store the values and if a collision occurs, increment the frequency counter. this can be done in O(n) both on time and space.

- Anonymous September 03, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hash table for time . BST for space ?

- XYZ October 27, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

sorting is not required.
just counting of occurrence is sufficient

- kim July 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

if we sort the list, didn't we destroy the list?

if we really really care about space, we can only keep a number and counter, go throught the list N times, find the max occurrence.

- Roger September 11, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Agreed

- Bullocks December 29, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Max Count is required.

I would compare : Max Heap and HashTable

Max Heap : Search for Max element O(1), insertion log(n) , space will total unique elements
HashTable: Search will be O(n) in worst case, insertion O(1) and space will be total unique elements.

BST: you can use bst everything will be O(log N) and spac will be total unique element.s

- kshitij July 21, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

//Find the majority element in O(n) by maintaining two pointers in array. 
findCandidate(a[], size)
1.  Initialize index and count of majority element
     maj_index = 0, count = 1
2.  Loop for i = 1 to size – 1
    (a)If a[maj_index] == a[i]
        count++
    (b)Else
        count--;
    (c)If count == 0
        maj_index = i;
        count = 1
3.  Return a[maj_index]

- technical_123 August 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

will this work on [2,2,1,3,4]?

- hs August 29, 2013 | Flag


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