Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Algorithm interview from Google

Tags:

algorithm

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 responsible for scheduling their concerts. Requests for performing at the Hall are accommodated on a first come first served policy. Only one performance is possible per day and, moreover, there cannot be any concerts taking place within 5 days of each other

Given a requested time d which is impossible (i.e. within 5 days of an already sched- uled performance), give an O(log n)-time algorithm to find the next available day d2 (d2 > d).

I had no clue how to solve it, and now that the interview is over, I am dying to figure out how to solve it. Knowing how smart most of you folks are, I was wondering if you can give me a hand here. This is NOT for homework, or anything of that sort. I just want to learn how to solve it for future interviews. I tried asking follow up questions but he said that is all I can tell you.

like image 567
NoNameY0 Avatar asked Feb 28 '13 02:02

NoNameY0


People also ask

What algorithms should I know for Google interview?

Algorithms you should know: Breadth first search. Depth first search. Binary search.

What are the 5 rounds of Google interview?

Google's recruitment process consists of five main parts: resume screening, phone screenings, on-site interviews, hiring committee reviews, and executive reviews.

How do I prepare for a Google programming interview?

We highly recommend you to go through CTCI (Cracking the Coding Interview) book, practice questions specially on GeeksforGeeks, Leetcode and Programming Interview Questions | CareerCup for Google interview preparation. You can also practice for the same on Google Preparation.

How hard are Google interviews?

Google coding interviews are really challenging. The questions are difficult, specific to Google, and cover a wide range of topics. The good news is that the right preparation can make a big difference.


1 Answers

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.

like image 188
comocomocomocomo Avatar answered Oct 01 '22 16:10

comocomocomocomo