Algorithm interview from Google

前端 未结 8 471
囚心锁ツ
囚心锁ツ 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:40

    Asymtotic complexity:- It means runtime is changing as the input grows. suppose we have an input string “abcd”. Here we traverse through each character to find its length thus the time taken is proportional to the no of characters in the string like n no of char. Thus O(n). but if we put the length of the string “abcd” in a variable then no matter how long the string be we still can find the length of thestring by looking at the variable len. (len=4). ex: return 23. no matter what you input is we still have the output as 23. thus the complexity is O(1). Thus th program will be running in a constant time wrt input size. for O(log n) - the operations are happening in logarithmic steps.

    https://drive.google.com/file/d/0B7eUOnXKVyeERzdPUE8wYWFQZlk/view?usp=sharing

    Observe the image in the above link. Over here we can see the bended line(logarithmic line). Here we can say that for smaller inputs the O(log n) notation works good as the time taken is less as we can see in the bended line but when the input grows the linear notation i.e O(n) is considered as better way. There are also the best and worst case scenarios to be seen. Like the above example.

    You can also refer to this cheat for the algorithms: http://bigocheatsheet.com/

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

    I've used a binary search tree (BST) that holds the ranges for valid free days that can be scheduled for performances. One of the ranges must end with int.MaxValue, because we have an infinite amount of days so it can't be bound.

    The following code searches for the closest day to the requested day, and returns it.

    The time complexity is O(H) when H is the tree height (usually H=log(N), but can become H=N in some cases.). The space complexity is the same as the time complexity.

    public static int FindConcertTime(TreeNode<Tuple<int, int>> node, int reqDay)
    {
        // Not found!
        if (node == null)
        {
            return -1;
        }
        Tuple<int, int> currRange = node.Value;
        // Found range.
        if (currRange.Item1 <= reqDay &&
            currRange.Item2 >= reqDay)
        {
            // Return requested day.
            return reqDay;
        }
        // Go left.
        else if (currRange.Item1 > reqDay)
        {
            int suggestedDay = FindConcertTime(node.Left, reqDay);
            // Didn't find appropriate range in left nodes, or found day
            // is further than current option.
            if (suggestedDay == -1 || suggestedDay > currRange.Item1)
            {
                // Return current option.
                return currRange.Item1;
            }
            else
            {
                // Return suggested day.
                return suggestedDay;
            }
        }
        // Go right.
        // Will always find because the right-most node has "int.MaxValue" as Item2.
        else //if (currRange.Item2 < reqDay)
        {
            return FindConcertTime(node.Right, reqDay);
        }
    }
    
    0 讨论(0)
提交回复
热议问题