Algorithm interview from Google

前端 未结 8 470
囚心锁ツ
囚心锁ツ 2021-01-30 11:55

I am a long time lurker, and just had an interview with Google where they asked me this question:

Various artists want to perform at the Royal Albert Hall and you are re

相关标签:
8条回答
  • 2021-01-30 12:19

    Store the scheduled concerts in a binary search tree and find a feasible solution by doing a binary search.

    Something like this:

    FindDateAfter(tree, x):
      n = tree.root
      if n.date < x 
        n = FindDateAfter(n.right, x)
      else if n.date > x and n.left.date < x
        return n
      return FindDateAfter(n.left, x)
    
    FindGoodDay(tree, x):
      n = FindDateAfter(tree, x)
      while (n.date + 10 < n.right.date)
        n = FindDateAfter(n, n.date + 5)
      return n.date + 5
    
    0 讨论(0)
  • 2021-01-30 12:23

    Assume, at level 1 all schedule details are available. Group schedule of 16 days schedule at level 2. Group 16 level 2 status at level 3. Group 16 level 3 status at level 4. Depends on number of days that you want to expand, increase the level.

    Now search from higher level and do binary search at the end.

    0 讨论(0)
  • 2021-01-30 12:35

    It was already mentioned above, but basically keep it simple with a binary tree. You know a binary tree has log N complexity. So you already know what algorithm you need to use. All you have to do is to come up with a tree node structure and use binary tree insertion algorithm to find next available date: A possible one: The tree node has two attributes: d (date of the concert) and d+5 (end date for the blocking period of 5 days). Again to keep it simple, use a timestamp for the two date attributes. Now it is trivial to find next available date by using binary tree inorder insertion algorithm with initial condition of root = null.

    0 讨论(0)
  • 2021-01-30 12:37

    You need a normal binary search tree of intervals of available dates. Just search for the interval containing d. If it does not exist, take the interval next (in-order) to the point where the search stopped.

    Note: contiguous intervals must be fused together in a single node. For example: the available-dates intervals {2 - 15} and {16 - 23} should become {2 - 23}. This might happen if a concert reservation was cancelled.

    Alternatively, a tree of non-available dates can be used instead, provided that contiguous non-available intervals are fused together.

    0 讨论(0)
  • 2021-01-30 12:38

    Why not try to use Union-Find? You can group each concert day + the next 5 days as part of one set and then perform a FIND on the given day which would return the next set ID which would be your next concert date.

    If implemented using a tree, this gives a O(log n) time complexity.

    0 讨论(0)
  • 2021-01-30 12:40

    Store the number of used nights per year, quarter, and month. To find a free night, find the first year that is not fully booked, then the quarter within that year, then the month. Then check each of the nights in that month.

    Irregularities in the calendar system makes this a little tricky so instead of using years and months you can apply the idea for units of 4 nights as "month", 16 nights as "quarter", and so on.

    0 讨论(0)
提交回复
热议问题