Goldman Sachs Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

min-heap?

- gnahzy October 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Heap wont work. consider a heap

______________1______________
   ________50____________100__________ 
____150_____170_____102____101___

for n = 3 u will get 1,50,100 which is correct.But for n = 5 you have to compare 150,170,102,101 which is the same problem on a smaller se.

- Noobie October 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Noobie, you only need to top n numbers, right? do we also need to store all numbers?

- gnahzy October 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Yes, we use min-heap but if n is much larger (around 1 million )then the heap will be a generalized heap.
Now when we get a new element we insert it a the bottom,(at the end), then we build it again, and we remove the last element. the same procedure follows for all element. Which means for one element costs us O(log(n)) complexity. Now when we extract element from heap, then we only extract the top element, then we replace the last element with it, then rebuild the heap on n-1 element, then again top and again the same procedure, So if we extract k data then we need O(k*log(n)) complexity.
Here extract means we remove that element from heap.
Now we only want to view the element then also we can do the same procedure, where the last position will be used to store the topmost entry,(nth entry of array(starts from 1)).

- sonesh November 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

running time of delete in min heap is O(n) which is not good

- DY November 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can someone please explain how min-heap can be used to store n- largest/top elements from the incoming stream of numbers? Thanks.

- xankar March 30, 2013 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

try with binary search tree

- sastry.soft October 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use RB trees

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

Use balanced search tree.

- Noobie October 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Make a search tree that stores number of elements in left and right subtree of each node.

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

there could be better ways but BST definitely would work. Storing each node is O(logn), printing descending order is O(n), insert and delete are both O(logn);

pseudo code for store:

initialize counter to 0;
code for Tree Node struct/class (int data, Node* left, Node* right);

store(int val) //called each time a new integer comes in
{
if(counter<=n)
{
counter++;
insert new node with value val in the bst; //O(logn)
return;
}

find the node with smallest value in the bst (the left most node); //O(logn)
if(val greater than left most node's value)
{
delete left most node; //O(logn)
insert new node with value val; //O(logn)
}
else //do nothing, the new val is not in the top n;

}

PrintDescendingOrder(node* root) //like in-order traversal, but right first, then self, then left
{
PrintDescendingOrder(root->right);
print root's value;
PrintDescendingOrder(root->left);
}

insert and delete is the same as inserting and deleting a value in bst......

- penny October 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Printing in descending order would be O(n)... you can't print N things in O(log n) time.

- jobeo October 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

updated my answer, lol no evidence that I ever said printing could be O(logn)....lol.... I hate typing uppercase and punctuations, so I copied O(logn) and forgot to change it to O(n)

- penny October 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use a Min Priority Queue of n elements..when the stream of integers are coming in,first store the n elements and after it,if (n+1)th element comes,remove the min element from the priority queue and add this new (n+1)th element..so at last we'll have the first top n elements :)

- Karthik Vvs October 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

but I guess sorting order wont be maintained

- LK November 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Min heap of size n.

- CST October 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Why not BinarySearch Tree ...?

- Anonymous November 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The answer is minheap of size n

- Sujith November 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Why not BinarySearch Tree ...?

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

we don't know the size of n ahead and we have to do insert and delete efficiently. Also we have to give a descending order of integer with out sorting, which means all n values should be stored in descending.

the best data structure to use is linked list along with a counter.

- Narendran s November 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

with BST, we cant have duplicate integers

- Pradeep December 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Why not hash map ?

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

boolean answer = isPanacea('HashMap')
Sysout(answer)

Output:
false

Hope you get this

- crankyCoder August 21, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

We might a need a combination of MinHeap and Balanced BST.

>> Track store top n elements
Use MinHeap to keep track of it.
Insert and delete is O(log n)

>> Should be able to display integers in descending order. Sorting should not be done whenever requested
Use a Balanced BST.
Insert, delete are O(log n)


Whenever you remove any node from MinHeap, remove it from BST too.

- Anonymous March 31, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

java:-Use Priority Queue.Peek element whenever new element is inserted ,poll it out else not change

- vikrant4.k April 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

java:-Use Priority Queue.Peek element whenever new element is inserted ,poll it out else not change

- vikrant4.k April 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You Should maintain a queue for size n and a BST.

Insert first n elements into queue and parallely to BST as well.

After n, insert element into the end of the queue and to BST. delete the old one from BST and queue.

For descending reverse inoder on BST.

Search is logn on BST and same with delete and insertion.

- Putta June 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You could be better then N*log(N). Let's use min heap, but compare new element with top. If data is uniformly distributed, we can expect not amortize time O(N). Let's consider worse case, increasing sequence. We can partially solve a problem by collecting block of data, shuffle it for O(N) and feed to min heap. If we can shuffle while sequence, we got O(N), if only block of M, got n*log(N/M).

Not sure what is requirements to not use sort for print, but want to mentioned sort of integers could be done for O(N).

Other approach, let's store all values in a bucket. Let's store min element from the bucket. If bucket has less then n elements, store incoming integer there. If it has more then 2n elements, find median and remove lower half for O(n) time. Otherwise store new integer if it's bigger then min element.

- Mike October 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

min heap will work fine, insert first n elements into heap, now stream of integer is on fire, as we get new integer we compare with min of min heap, if it is more than this we remove min and insert this new element so time complexity for one insertion will be lon(n), let total stream integers are G so over all time complexity will be G.lon(n), million no's, means 10^6 elements so min heap will work fine.

- erkejriwal June 20, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Java has a LinkedHashMap for this purpose. You can just override the removeEldestEntry() method to keep the required number of elements. So i guess a satisfactory answer would be a hashmap combined with a doubly linked list.

- hunterXcoder July 04, 2018 | 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