Yahoo Interview Question for Software Development Managers


Country: United States
Interview Type: Phone Interview




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

1) man, I think min-heap is better for this problem.

2) maintaining these values in memory
a) using multiple machines, each handles requests of a period of times slots
or
b) Memory might be saved considering the fast that some stock value can be top 5 in a time range. Can anyone suggest a good data structure to handle this case.

- Jason November 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

How min-heap will be better than max-heap in this case?

- ami November 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

min heap would be a 5 element fixed storage
max heap would be ALL data seen so far as storage

Jason correct

- {{{ /* ninja code goes here */ }}} November 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

what abt two variables that hold min value and index of min value and a 5 elements array for stocks.every time a stock update comes if it's greater than the min value then update the array..tell me whether it is gonna work?

- @@@@@@@ November 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

sorry it seems a min-stack is also needed.

- @@@@@@@ November 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Stock {
  String ticker;
  double value;
}

double lowesetIndex = 0;
double lowestValue = 0;
int numStocks = 0;

Stock[] topStocks = new Stock[5];

HashMap<String, Integer> stocksMap = new HashMap<String, Integer>();

void updateStream(Stock newQuote) {
  if (numStocks < 5) {
    // Add it to current index and just return.
    topStocks[numStocks] = newQuote;
    stocksMap.put(newQuote.ticker, numStocks);
    
    if (newQuote.value < lowesetValue) {
      lowestValue = newQuote.value;
      lowestIndex = numStocks;
    }
    
    numStocks++;
      
    return;    
  }
  
  if (newQuote.value > lowestValue) {
    if (stocksMap.hasKey(newQuote.ticker)) {
      int index = stocksMap.get(newQuote.ticker);
      topStocks[index].value = newQuote.value;
    }
    else {
      topStocks[lowestIndex] = newQuote;
    }
    
    calculateLowest();
  }
}

void calculateLowest() {
  lowestIndex = 0;
  lowestValue = topStocks[0];
  for (int ii = 1; ii < topStocks.length; ii++) {
    if (topStocks[ii].value < lowesetValue) {
      lowestValue = topStocks[ii].value;
      lowestIndex = ii;
    }  
  }
}

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

What happens if one of the top 5 stocks fall below the lowestValue ?

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

(1)On most part, I agree with the originator, such as storing data in a maxheap, doing sift to update.
(2)But in terms of picking top 5, there might be a simpler way to do that, I guess.
(3)We could build the maxheap with just a plain array and maintain it by ourselves. Then the GUI needs to be updated only when the first 5 items of the array is changed.

- uuuouou November 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Construct a min heap of 5 elements, so that will ensure we have minimum element as the root of the heap.Whenever a new ticker comes by, we will compare it with root if it greater then we do a MinHeapify step else ignore it.
O(k) -Build Min heap
O(log k)- Minheapify

k=5.

- Akhil February 15, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

you should use min-heap to keep track of max k elements at given time.

- alien April 01, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We will need a min-heap and a HashMap.

1. Min-heap will contain top 5 stocks.
2. when new stock comes in,
- if it is greater than min-heap, then replace the top of min-heap by this stock and down-heapify (O(log 5))
- if it is less than the min-heap, then just update/insert the value of the stock in the HashMap (O(1)) (HashMap will contain all data : not present in min-heap + present in min-heap)
- if old value of stock is present on the min-heap and the new value is less than the top of min-heap, then construct a max-heap from HashMap and take the max element into the min heap (heap can be constructed in amortized O(n))

- nharryp November 04, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The min/max heap approach is good but , we can think it other simple way only. As this is the streaming and need to store only top 5 values at any given point of time, we can have a double array of 6 element which will store the values while read each stock data in a loop.
Time complexity should be O(nk) where K is limit, here 5.
C++ solution:

// Here size is streaming data size, limit is to be passed as 5
double* topFiveElement(double* Data, int size, int limit)
{
    double *ptr = new double[limit+1];
    for ( int index = 0; index <= limit; index++)
    {
        ptr[index] = 0.0;
    }
    
    // Read each value from stream
    for( int i =0 ; i < size; i++)
    {
        // store it into a predefined array in decending order. Iterate at max five times only
        ptr[limit] = Data[i];
        for( int j = limit; j > 0; j--)
        {
            // Swap values if right element is larger
            if( ptr[j] > ptr[j-1])
            {
                double tmp = ptr[j-1];
                ptr[j-1]   = ptr[j];
                ptr[j]     = tmp;
            }
            else{
                // if the above condition does not staisfy exit the loop
                break;
            }
        }
    }
    return ptr;
}

- som March 15, 2017 | 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