amazon 面经3

http://www.geeksforgeeks.org/amazon-interview-set-107/

 

F2F-I:
1) Brief discussion on work in current company
2) Flatten linked list – http://www.geeksforgeeks.org/flatten-a-linked-list-with-next-and-child-pointers/

The problem clearly say that we need to flatten level by level. The idea of solution is, we start from first level, process all nodes one by one, if a node has a child, then we append the child at the end of list, otherwise we don’t do anything. After the first level is processed, all next level nodes will be appended after first level. Same process is followed for the appended nodes.

1) Take "cur" pointer, which will point to head of the fist level of the list
2) Take "tail" pointer, which will point to end of the first level of the list
3) Repeat the below procedure while "curr" is not NULL.
    I) if current node has a child then
    a) append this new child list to the "tail"
        tail->next = cur->child
    b) find the last node of new child list and update "tail"
        tmp = cur->child;
        while (tmp->next != NULL)
            tmp = tmp->next;
        tail = tmp;
    II) move to the next node. i.e. cur = cur->next 

3) Design a data structure which holds number 1 to n such that insert, remove(this operation will take in a number between 1 to n as argument and remove that number from data structure if it exists) and get valid element in the data structure operations are done with O(1) complexity

what is it the maximum number of the elements 

combination  array and hashtable 

 

Problem Statement

Design a data structure that allows insert, delete and getRandomElement() 
in constant time i.e. O(1).

Solution
distinct number Insert and delete in O(
1) needs a hashmap and get random element on O(1) suggests some kind of array access. We can take a HashMap H and an array A with max size. Now HashMap should have key as the element to be inserted and value as the position of the element in the array, also keep a last pointer telling the index of the last element in the array. Insert is simple, increment last pointer and add the new element at last position and add in the hashmap. Delete is a little tricky, First get the position of element in the array from hashmap, remove the element from hashmap, swap A[pos] and A[last] in array, decrement last pointer and update the position of swapped element in the hashmap. delete(element){ int pos = map.get(element); map.remove(element); Swap A[pos], A[last]; last--; map.put(A[pos], pos);//update position to new pos } getRandomElement() is simple, get a random number between 0 and last(both including) and return A[random number]

 

 

F2F-2:
1) Brief discussion of work in current company
2) Find and print longest consecutive number sequence in a given sequence

 

    Ex: Input: 1 2 5 3 6 8 7
       Output: 5 6 7 8 

public class Solution {
    public int longestConsecutive(int[] num) {

        Set set = new HashSet();
        
        for (int i : num) {
            set.add(i);
        }
        int max = 0;
        
        for(int i=0; i){
            if(set.contains(num[i])){
                int next = num[i] - 1;        // 找比num[i]小一个的值
                int count = 1;
                set.remove(num[i]);            // 及时的移除,减少之后的查找时间
                while(set.contains(next)){
                    set.remove(next);
                    next--;
                    count++;
                }
                next = num[i] + 1;        // 找比num[i]大一个的值
                while(set.contains(next)){
                    set.remove(next);
                    next++;
                    count++;
                }
                max = Math.max(max, count);
            }
        }
        
        return max;
    }

3) A fair die is thrown k times. What is the probability of sum of k throws to be equal to a number n?

 

F2F-3:
1) Brief discussion of work in current company. Why Amazon?
2) Why do you want to leave current company? What do you like most and dislike most about your current company?
3) Sum two numbers represented by linked list iteratively and recursively.

public class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode c1 = l1;
        ListNode c2 = l2;
        ListNode sentinel = new ListNode(0);
        ListNode d = sentinel;
        int sum = 0;
        while (c1 != null || c2 != null) {
            sum /= 10;
            if (c1 != null) {
                sum += c1.val;
                c1 = c1.next;
            }
            if (c2 != null) {
                sum += c2.val;
                c2 = c2.next;
            }
            d.next = new ListNode(sum % 10);
            d = d.next;
        }
        if (sum / 10 == 1)
            d.next = new ListNode(1);
        return sentinel.next;
    }
}

 


4) You are given an infinite sorted array containing only numbers 0 and 1. Find the transition point efficiently.

divide and conquer

 

 

1) Lots of HR, behavioral and team fit questions
2) User statistics are logged in the following format –

    user_id|page|time at which page was accessed
   We need to identify most followed 3 page sequence by users.
   Example:
      Input: U1|Page1|05/08/2014 10:00
               U1|Page2|05/08/2014 10:01
               U1|Page3|05/08/2014 10:02
               U1|Page4|05/08/2014 10:03
               U2|Page2|05/08/2014 10:02
               U2|Page3|05/08/2014 10:04
               U2|Page4|05/08/2014 10:05
               U3|Page3|05/08/2014 10:04
               U3|Page4|05/08/2014 10:05
               U3|Page5|05/08/2014 10:06
      Output: Most followed 3 page sequence for the input is 
              Page2 -> Page3 -> Page4. 
Userid  PageID
A          1 A          2 A          3 B          2 B          3 C          1 B          4 A          4


Find the most frequent visit sequence of page-ID:

 for A : 1-2-3, 2-3-4 for B : 2-3-4

so, 2-3-4 is the most frequent.

 

 

smaill elements:

Put each item of the file into map1 >.
When list.size() == 3, create a new struct three_hits to hold the three pageID.
Put it in into map2int counter>.
Then, find the item in map2 with largest counter value.

 

huge elements:

If you want to quickly get an approximate result, use hash tables, as you intended, but add a limited-size queue to each hash table to drop least recently used entries.

If you want exact result, use external sort procedure to sort logs by userid, then combine every 3 consecutive entries and sort again, this time - by page IDs.

Why not just cut the log file into slices that are each big enough to hold in memory

 

转载于:https://www.cnblogs.com/leetcode/p/3898513.html

你可能感兴趣的:(amazon 面经3)