现在的位置: 首页 > 综合 > 正文

A家面经

2017年12月23日 ⁄ 综合 ⁄ 共 2487字 ⁄ 字号 评论关闭

Round 1 (Written)
1. Given an array, output an array where every index contains nearest 
greatest element to that element on right side.
2. Program to convert sorted array to Binary Search Tree

3. Find first non-repeating character in String

思路:后两题貌似LC上都有,第一题也比较简单,从右往左扫描一直记录最大值就是了(如果nearest greatest没有理解错的话)


Round 2 (F2F)
1. Given linked list as a-x-b-y-c-z
output it as a-b-c-z-y-x
that is reverse alternate element and append to end of list

2.
Output nearest number greater than given number such that output is 

palindrome
ex: 121:131
900:909
99:101

思路:也毕竟简单,用两个表头h1和h2,然后扫描原链表,依次分别添加到h1和h2上,只是h1正添,h2逆添就是,最后把h1和h2后的表连起来就是。

第二题比较麻烦,如果每次累加1然后判断是否回文是可以的,但是效率不高。我的思路是,1:如果数字是999这种全9的形式,单独处理,也比较好处理,变成1001这种形式即可;2:对于给定的N,分成3部分,首数字a,末尾数字b,和中间部分m;如果a>=b,则把b增大到a,判断中间部分m是否是回文,如果是,则变换完毕;不是,则再同样的方法处理中间部分m;如果a<b,则判断中间部分是否是9999全9的形式,如果是,则把a增大到b,中间部分m全变为0,否则把b变小为a,在同样方法处理中间部分m。


Round
3 (F2F)

1. Vertical Sum in Tree( I told him I know the solution, he proceeded 
further)
2. Given stream of Strings find top 5 words with maximum frequency or count
3. Given 2 nodes in Binary Tree find distance between them

思路:第一题不是很明白,第二题用map或者字典树就好了,字典树应该节省空间一点,但是注意字典树的节点设计,每个字典树节点添加一个数组,这个数组存放的是这个节点为根往下的所有树中,频率最大的5个单词,同时对应5个单词的出现次数,这样在添加一个单词到字典树的过程中,我们还可以同时更新该节点的5个单词,最后添加完毕直接查询根节点即得,缺点是占空间,同时5个单词也比较不宜扩展;当然每个节点用一个变量保存出现次数,最后扫描去最大的5个也可以。

第三题的话,思路一个把树看做图,然后从一个节点开始做BFS,一直到另一个节点入队,这个BFS的层数就是两个节点的距离,优点是时间O(n),但可能要建图浪费空间;另一个思路则是先找两个的最近公共祖先,这是比较常见的题目了,O(n)时间,然后分别计算两个祖先到两个节点的距离,和即是。


Round 4 (F2F with
hiring manager)

1. Projects done so far, HR questions
2. Design Linkedin and find till 2nd level connections and path between 2 
connection
for ex: if A is friend of B which is friend of C
print between A and C A-B-C
3. Programming language: Java
About synchronisation, serialization, transient and volatile keyword, 
Singleton Class

哎,设计题我都好搓啊。。。


Round 5 (Bar Raiser)
1. Count Inversion in array that is if i a[j]
Told the solution nlogn of divide and conquer. He asked another solution, 
then told by inserting in BST and whenever node goes to left side then 
adding 1 and number of children on right side . We have to keep track of 
count of right subtree in every node

思路:逆序数统计,也是很常见,常见的解法是类似归并排序,在归并的过程中统计,另一个方法是先离散化,插入线段树,在另一篇线段树的文章中有讲过。这里写的BST的方法跟线段树类似,只是树的节点设计中,每个节点要一个标记表示右子树的节点个数,表示在插入当前数时候,前面已经有多少个数比它大了,上面不应该是adding
1,而应该是该节点+该节点右子树节点个数,注意插入时更新右子树节点个数变量即可。


Round 6 (F2F)
1. HR questions (Why leaving company, projects, SWOT)
2. Program to check for mirror tree
3. Data Structure so that push, pop, getmin, getmax O(1) (using 3 stacks)
4. Data Structure so that push, pop, pop min, pop max
Told Solution till O(logn) by using min heap, max heap with pointers to 
doubly linked list nodes

思路:第二题是LC题,第三题看要求的是栈还是队列,只需要在原来的栈或者队列之外,再加两个单调栈或单调队列即可。

第四题看题目有点奇怪,如果pop
min,pop max,不是就破坏栈或队列的性质了吗,如果要的不是栈或者队列,那push和pop怎么理解?

抱歉!评论已关闭.