博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
amazon 面经3
阅读量:7232 次
发布时间:2019-06-29

本文共 5835 字,大约阅读时间需要 19 分钟。

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 list2) Take "tail" pointer, which will point to end of the first level of the list3) 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 numberInsert 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

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  PageIDA          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 map2
.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

你可能感兴趣的文章
about
查看>>
O-Bomb(数位dp)
查看>>
hdu5032 点和查询分别按极角排序 离线做+树状数组
查看>>
hdu1428 递归形式dp(记忆化搜素):A能到B的条件是A到目的地最短路大于B到目的地最短路...
查看>>
实例详解Django的 select_related 和 prefetch_related 函数对 QuerySet 查询的优化(三)...
查看>>
关于VC++6.0 MFC项目运行所需的动态链接库
查看>>
由system.currentTimeMillis() 获得当前的时间
查看>>
复习日记-Linux项目发布
查看>>
The 'Microsoft Jet OLEDB 4.0 Provider' is not registered on the local machine
查看>>
Java 基础源码 switch语句判断指定月份属于一年中的哪个季度
查看>>
12px以下字体显示问题
查看>>
小程序滚动条 无法滚动BUG 解决方案
查看>>
cs108 04 oop design
查看>>
win7 打开方式不能添加程序
查看>>
EasyUI-panel 内嵌页面上的js无法被执行
查看>>
pycharm运行input输入字符串报错 NameError: name 'xxx' is not defined
查看>>
微信小程序rpx单位
查看>>
Javascript读写CSS属性
查看>>
58.com qiyi
查看>>
ORACLE批量导入图片到BLOB字段
查看>>