2。给一字符串比如“I like FaceBook”,计算单词数目。
3。写quicksort算法
1. 一个rotated的排序整数数组,比如A=[6,8,1,2,4,5],写code找一个给定元素,并分析复杂度。其实就是binary search的变体,但是需要考虑两种A[m]中值的情况加以判断。
2. 他谈到facebook的log,如果每个log文件有10 billion行,每行包括timestamp,
user_id, visited page三个field。如果高效的统计一个月内用户访问量最多的十个网
页。假设文件已经按照timestamp排好序了。不用写code,谈想法就成。我的解法是,
如果内存足够,建立一个<网页,访问数>的hash表,每读入一条记录,该网页key对应的
项加一。但是加入内存不够大,可能需要map reduce方法。面试官马上说,内存根本不
是问题,用个4G或者8G内存来算这个月访问量很值得,不要省内存。然后我说,那hash
表就好了。面试官表示赞许,接着提出引申问题:
3.假设我们已经算了一个月的,如果这个滑动窗口要往前移动一个delta量,就是说,
已经算好timestamp=[t_1..t_x]区间的访问量,要计算timestamp=[t_2..t_x+1]的访问
量,提出高效算法。当然,最brute force的方法就是针对[t_2..t_x+1]再建一个hash
表,显然这不是面试官想要的。我提出来,可以建个multi-level hash表,外层为网页
,内层为timestamp,但是仔细想想也不算好。
1. find 5th smallest element in a binary search tree。地球人都知道,我就不说了。
3。然后问我给定一个FaceBook log file,100 billion行,每一行记录含“timestamp
user_id visited_page”,找到top 10 最长出现的三连串访问模式。比如user先后访
问了页面a,b,a,那么就形成一个模式a->b->a。但是记录没有按照timestamp排好序!
我的答案是,1)brute force:对整个文件先对timestamp再对user id排序,然后建
hash表,读入log文件,对每一个模式计数。 2)map-reduce:将每一个user id对应的
记录读到单独的机器上--map,接着对timestamp排序建hash表,这样效率高些,然后--
reduce,将intermediate结果对每个模式key累加,计算最后结果。然后因为只需要top
10,可以用一个10个元素的min heap维护当前top 10。哥哥满意。
1. 如何在一个无限长的stream里面找到前1000大的,因为是非常常见的题目,就和他说了一下用min heap,分析了一下复杂度,很快就进入coding了
2. simple regular expression match,可以match的符号只有3种:a-z: match a-z .: match any *: repeat 0 - arbitrary times
1. 先找出所有permutaion,coding之,然后find permutation by index
比方说给定一组permutation(a,b,c)是:0. abc 1. acb 2. bac 3. bca 4. cab 5. cba given index 3,you should retun bca.
1. 二进制加法
2. 分层遍历二叉树
1. reverse linked list
1. how to implement adding two integers without using arithmetic operators. (bit-wise operations)
2. how to implement a writer-reader-lock while you can only use mutex.
1. atoi, write exact code
2. 电话键盘上每个数字对应一系列字母,给任意长度电话号码,打印所有可能字母组合。基本就是PIE上的原题
1. Two sorted array, find their intersection, write exact code
2. Part of a sorted array is shifted, like: 1 2 3 4 5 6--> 6 5 1 2 3 4. but we don't know how many digits are shifted. Write a efficient method to find the position of a given value in the array.
1. 按逆顺序打印一个single linked list。用stack, 直接reverse 链表:分别code然后他提到不改变原有的链表也不用额外的空间.. n^2的方法。
1. 如何判断一个system stack的direction?
1. atoi
2. sqrt()
1. 一个是inplace reverse linklist
2. 另一个是strcmp的implementation。
1. If a hashtable is bounded, how do you modify the table so that,the item being added earliest ( now in the full table ) would be removed when trying to adding to the full hashtable. interviewer当然没有能够把问题formulate好,举了个例子来说明。中间我来回让他重复了好多次。他的意思是 如果hashtable里面已经按次序加了 (1,2,3,4,5)而且满了,那么加6的时候,1就应该被删除; 再加7, 2就应该被删除。要求普通hash操作还是O(1)
2. Implement Merge Sort. 这个没什么好说
3. SQL 一个table, 一个employee 在不同的timestamp下会有不同的salary. Employee Salary TimeStamp. Find Employee A's salary with largest time stamp
1. 一个数组,positive / negative integer, find consecutive numbers that have maximized sum . And code it. [-5,5,5,-5,5,5,-10,]就应该是 5,5,-5,5,5
2. A number of events for a day, starting time and end time find a solution that maximizes the number of events one can finish in the day. Events may over lap with each other.
1.implement strstr()
2.如果很多次strstr query,但是base的string不变的话,用什么structure,如果base string大到内存放不下,那用什么structure。suffix tree, Btree
1.二叉树分层打印,每层打印完后换行
2.两个sorted的array,第一个有足够大的剩余空间装下第二个,merge两个array到第一个array中
1. 给一些单词,找出由相同字母组成的单词并分别打印出来 比如 给一个string 的array "act", "cat", "star", "arts" ,"delegate" 输出结果应该是:act, cat star,arts delegate
2. 按如下规则产生字符串:给一个参数n,写函数返回第n个字符串 初始字符串 为 "1" ,因为这个字符串中从开始扫描有一个1,所以第二个字符窜为 "11" , 第二个字符串“11”从头开始扫描有两个1 所以第三个字符串为“21” 然后第三个字 符串从头开始扫描有一个2 一个1,所以第四个字符串为“1211”,第四个字符串从头开始扫描是一个1一个2两个1 所以第5个字符串是“111221”.... 题目就是给一个参数n,找出第n个字符串应该是什么
1
11
21
1211
111221
...
1. 怎么判断两个linkedlist meet
1. 有个矩阵,里面都是0和1,找最大的cluster
1. 打印整数的byte
1. 经典最长子序列问题(LIS)
1.1. 反转单链表
1.2. 广度优先遍历一颗树
2.1. 实现一个strstr函数
2.2. 输入: phone # 输出: 所有对应的string. eg. 输入: 23, 输出:[ad, ae, af, bd, be, bf, cd, ce, cf]
1. 给一个binary tree,按照BFS的方式打印出来。
2. 一道是strstr,
3. 还有一道是实现sqrt。
1. 求集合a b的 Intersection. a = {1, 2, 99, 101} b = {-1, 0, 2, 100}
1. given two integer numbers n and k, print all combinations of k numbers out of 1 ... n. e.g. if k = 2, n = 4, print
1 2
1 3
1 4
2 3
2 4
3 4
2. if you have a grid
a b c e
d f a h
i j k l
and a dictionary with more than 10K words print out all words in dictionary that can be obtained by visiting grid. restriction: if a word is c1, c2, ..., cn, then c_(i+1) should be one of the eight neighbors of c_i in the grid
1. 一个是给定两个linkedlist, 判断一个是不是另一个的prefix.
2. 实现一个hashtable
1. 给定 X[1..n] and Y[1..m] 两个 arrays,已经sort好了. 如何找到X Y的median?我说用merge sort,要O(m+n/2)时间。面试官明显不满意。这个已经 lineal了?难道还有更快的?
1. How to use stacks to simulate queue. (do not use online tool, just write and tell him. Use two stacks).
2. How to find the lowest common ancestor of a binary tree, node do NOT have parent pointers. (Recursion, additional check for the case when nodes are not in the tree, or only one node is in the tree.)
3. Given a linked list, say A->B->C, print it in reversed order. Time & space analysis. What if I want the original list not changed?
1. 怎样de-dup一个sorted array?
2. 如果有一个big single file, many servers, how to use these servers to compute this problem? 要求尽量balance load. 我有点晕,这个听上去有点太简单了,不知道有什么trick...但是还是老老实实说每个server 读一部分file,分别计算,最后用个很简单的merge就可以了。还可能让第一个CPU先算一部分,用它的data来估计大概有多少duplication,如果很多,而且有相应的optimized algorithm,后面的server可以一次多算点
3. 怎样check circular in a linked list
4. merge two sorted array的问题,如果array A有足够空间,如何in-place merge A and B (both sorted)
1. 第一道二叉树分层打印,他告诉我20分钟完成就好了,我只用了15分钟就完全写好,我敢保证绝对正确。随后,他问算法的复杂度是多少,我说是O(N)
2. 第二题,题目很新,但是可以马上转化为一个在有大量重复整数的有序数组,找到第一个出现的某个整数。
1.1 LIS的变种。Give an array, find the minimum of elements to delete so that the remaining array is sorted.
2.1. How to find the middle node of a binary search tree?
2.2. implement char *remove_badchars(char string[], char bad_chars[]) in place。分析time complexity and how to improve (binary search on bad_chars)
2.3. implement siftup (siftdown) for heap
3.1. traverse a binary tree level by level
3.2. implement adding two unsigned numbers without using "+"
o.1. implement sqrt
o.2. implement reader/writer lock
o.3. classic question: print phone numbers
o.4. How to sort an array of only three possible values。面试官说有一个只scan array once的算法(类似quick sort的partition algorithm from two ends)
Tree & Linkedlist
1. Given a binary tree with the following constraints: a) A node has either both a left and right child OR no children b) The right child of a node is either a leaf or NULL. write code to invert this tree.
2. DSW
Arrays & String
1. Given two sequences of items, find the items whose absolute number increases or decreases the most when comparing one sequence with the other by reading the sequence only once.
Other
1. Given n points in the form (x1, y1, z1)…..(xn, yn, zn), find the k closest points to the origin. Given the same points as above, find the K closest points to each other.
2. Convex Hull
3. Closest Pair
4. Vertex Cover: find min number of nodes that cover all edges. (backtracking)
5. K coloring (backtracking)
6. K closest pairs
7. Given a repository of 10 billion pages and each page has an average of 10 links, how will find its neighbors and neighbors of neighbors?
8. Design an algorithm to find the 100 shortest distances to stars in the universe. (KD tree?)
Maths
1. LCA & RMQ
2. Implement a calculator
Questions in the Placement
1. Unsorted array - Find i, j such that a[i] + a[j] is max. Find solution better than O(nlgn).
2. Partitioning a given integer n into unique partitions of size m, like if n = 10, m = 4, 7111 is one such partition. Give all such partitions.
3. Given 2 arrays of arbitrary sizes N and M each. Write a program for finding mutual exclusion of these two arrays. (Copy the non-common elements into another array).
4. Find a saddle point in matrix. (smallest in its row and largest in its column)
5. Given sorted array and an integer C, find 2 elements a[i] and a[j] such that their difference is C. O(n)
6. A matrix, print it from top-left anti-clockwise
7. Given 4 points write a program for finding whether the points form a square or not. -- Angles, Sides, diagonal three aspects of looking at it.
Questions from Avidullu
http://avidully.wordpress.com
17.1. Given that you have one string of length N and M small strings of length L . How do you efficiently find the occurrence of each small string in the larger one ?
17.6. Given a list of machines where each machine has a hard disk limit and memory capacity and given a list of processes where each process requires certain hard disk space and memory, write an efficient algorithm to match processes to machines. (You may assume process to server mapping is 1:1).
17.7. A container is filled up with balls. Each ball has a value. Red = 7, Yellow = 5, Green = 2, Blue 2. Some balls are removed from the container and the product of their value is 147,000. How many red balls have been removed?
16.2. A file is given with many 0s stored in continuous way , store it in another file such that when you store try saving the space by using minimum amount of space. When you want to create the original file , you should be able to do so with the new file created.
16.3. Propose a data structure that would store numbers, without any knowledge about them, and allow to perform the operations: insert, get median, as efficiently as possible. Modify/optimize your design, only this time the numbers are from a group V, which is |V|<
16.4. Given N computers networked together, with each computer storing N integers, describe a procedure for finding the median of all of the numbers. Assume that a computer can only hold O(N) integers (i.e. no computer can store all N^2 integers). Also assume that there exists a computer on the network without integers, that we can use to interface with the computers storing the integers.
15.2. Given a char array with words in it, find all ‘a’ characters and replace with xyz. Modify the input array, do not create a copy of the array.
15.3. How do you output the nodes from a binary search tree given a range
15.5. Write a program to arrange numbers from 1 to 16 such that the sum of two consecutive numbers is a perfect square.
14.2. Given a BST, print all the values >=vmin and <=vmax. Time complexity should be better than O(n). O(logn + (vmax-vmin)) in worst case. No parent pointers are available.
14.4. Write a function: int count_div(int a,int b,int k);
That given three integer numbers a, b and k return number of integers from range [a..b] divisible by k, i.e.: For example, for a=6, b=11, and k=2, your function should return 3, because there are 3 numbers divisible by 2 in the range [6..11], namely 6, 8 and 10. You can assume that , and k>0.
14.5. Given is an array of integers. Element is called an extreme if no other element’s value is more distant from the average. Write a function
int extreme(int[] A);
That given an array of integers returns the index of an extreme (any one of them if there are many).
If no extreme exists, the function should return -1.
A[0]=9, A[1]=4, A[2]=-3, A[3]=-10
the index of an extreme is 3, because the average of the array is
(9 + 4 + (-3) + (-10)) / 4 = 0
and in this array no value is further from 0 than -10
14.6. Say you need to design a web application which needs to support friends of friends function(like in linked in, when you search a person, it will show you if this person is linked with you, your connection or your connections’ conection…), we expect to have millions of users and each user may have thousands of friends, how would you design/implement this function to make it scalable.
14.7. Given two n digit prime numbers, change the first number to the second by changing one digit at a time. The resulting intermediate numbers should also be prime.
14.10. Given a list of strings say N, how would you write a perfect hash function and ensure 0 collisions?
13.4. Given a 3d plane, and infinite points find kth closest point from a given point.
13.10. Given a dictionary of words and a string with all spaces removed, return whether the string is composed of valid words
e.g
helloworld-> hello world (valid)
isitniceinhere-> is it nice in here (valid)
zxyy-> invalid
13.4. Write pattern matching algorithm.
i.e isMatch(String,Pattern)
Pattern can have ?(one character) and *(many characters).
ex. abbdef and a?b*f are a match
aaabab and a*ba are not a match
13.5. Pretend there is a robot that has to navigate a maze (N x M). The robot can only move down or right and the maze can contain walls. Write an algorithm to determine the number of paths the robot can take.
13.6. Write a function to find the nearest link on a webpage given the mouse x,y coordinates. If your algorithm just iterates through all the links, give an idea of how to make it faster.
13.8. There is a byte array which contains the character of one byte and two bytes. One byte character has range 0 to 127 and first character of 2 byte character is 128 to 255 and second byte character has range 0 to 255. Now, two pointers are given, one points to the start of the and another points to somewhere else. Tell which character 2nd pointers points to?
13.9. You have a graph with and start point A and end point B. All edges have positive weights. Now the shortest path from A to B can be found using Djikstra.
a) Find the kth shortest path
b) Find the longest path
12.2. How will you go about implementing google suggest kind of functionality for a cellphone.
12.7. You have millions of documents numbered 1,2,3,4,5 ……
You also have a mapping from a word to the list of documents that contains it.
Find all the ‘pair of words’ that occur together in one and only document.
12.8. How will you find the page with most incoming links from billions of web-pages.
11.1. Given 10K of 16bit integers and unlimited memory, find most efficient algorithm that counts number of bits set to 1.
11.3. Print elements that are in a vertical column (per column) in a Binary tree
11.4. Given customer vs visited pages log for three days, find the customers who have visited for exactly 2 days and visited page count > 2. Customer can visit the same page any number of times.
11.6. Palindrome Date: A date is said to be a palindrome when it is expressed in MMDDYYYY format, it reads the same both ways. Given 2 years as input(ex: 2000, 2010), print out all the dates which are palindrome dates.
11.8. Write a preorder traversal of a binary search tree w/o recursion / stack / extra memory storage. Hint – you can augment the node data structure. However, you can’t add a visited field to the node.
11.11. An array is given like {1,4,5,2,3,6,7}. sort the odd elements in descending order and even numbers in ascending order. so result would be {7,5,3,1,2,4,6}.
10.2. You are given a set of n points in a XY plane. Suggest an algorithm to determine if every point is at least separated by every other point by a Manhattan distance of 5 units. Return should be true or false. Better than O(n^2).
10.7. Write a function that takes an array of five integers, each of which is between 1 and 10, and returns the number of combinations of those integers that sum to 15. For example, calling the function with the array [1, 2, 3, 4, 5] should return 1, while calling it with [5, 5, 10, 2, 3] should return 4 (5 + 10, 5 + 10, 5 + 5 + 2 + 3, 10 + 2 + 3). You may assume that the input has already been validated. Show how you would test this function.
9.1. How to find two vertices of a polygon which are farthest from each other in linear time.
9.2. Given a string find the number of distinct substrings of the string.
ex:
input-> aaaa
output-> 4(a, aa, aaa, aaaa)
input->abcd
output->10(a, b, c, d, ab, bc, cd, abc, bcd, abcd)
9.8. Given a number line 1734, how to return the next higher permutation of its digits.
9.9. Sort the input array. Only following operations on array is allowed:
1)get(index) -gets the element at that index
2)reverse(int start,int end) – example reverse(1,3) for the array [1,2,3,4,5] will return [1,4,3,2,5]
9.10. Given a large set of balls say N such that these balls are identical if they are of the same color. We have to randomly pick one of the balls such that the probability of picking is the same. Find an efficient way of solving this in terms of space and running time complexity.
8.1. Find the longest common subsequence of given N strings each having length between 0 to M.
8.3. Given a bst of n nodes, find two nodes whose sum is equal to a number k in O(n) time and constant space
8.4. We have N element array with k distinct keys. sort this array without using any extra memory.
8.5. You buy a newspaper and notice that page 8 and 21 are on the same sheet. Can you infer the total number of pages in the newspaper?
8.6. Given two sorted postive integer arrays A(n) and B(n) (let’s say they are decreasingly sorted), we define a set S = {(a,b) | a \in A and b \in B}. Obviously there are n^2 elements in S. The value of such a pair is defined as Val(a,b) = a + b. Now we want to get the n pairs from S with largest values. in O(n).
7.2. Given a polygon and a point, you have to find that whether the point is inside the polygon or outside.
7.3. How to identify bits that alternate 1 and 0. Ex: 101010, 10101, 01010, 01
7.5. What has to be done in order to be able to do a O(logn) binary search on a linked list.
7.6. Given a sorted array (ex. {1,2,3,4,5,6,7,8}). A random number from the array is taken and put the left side array to right side and left side array to right side. In the example; if 6 is randomly taken, the array will become like {7,8,6,1,2,3,4,5}
Question:
Given the above mangled array, write a searching algorithm to find a number present or not. That algo should not be of O(n) solution.
7.7. Write a function which takes a number as input and returns it next number. Condition: the next and given number should have equal number of 1′s in its binary format.
Example: 5 – 101 its next number 6-110. both have two 1′s.
6.1. Given an array of integers A[N], find the maximum value of (j-k) such that A[k] <= k and j > k.
6.4. Given a>0 can you compute a^-1 without any division?
6.5. 100 digits are chosen at random. What is the probability to find two numbers, a and b, such that a^2=b and each digit is used just one time in forming the two numbers?
6.6. You are given a sequence of m distinct numbers extracted by a set of 1…n numbers, with m < n. Can you give an efficient algorithm for identifying a number non in the sequence.
6.8. Given an array of integers from 1 to N, and given a number X, how many ways are there to pick X elements from the array such that no two elements in the selected X elements are consecutive.
6.9. Given a log file which has customer id and corresponding to that id it has a page id visited by that customer. Given such log files for 3 consecutive days, design an algo to find those customers which visited the site on exactly 2 out of 3 days and visited at least 3 distinct pages. Discuss the design and space complexity. Optimize (question is not about using unix tricks)
6.10. Given a log file with first field being an url and second being the render time of that page. find the page with least render time. Url is accessed many times like 50 times or 100 times. every access has an entry so we have to find the average.
6.11. Which is the DS used in dictionary mode in mobile (t9)
6.12. Write a program which given a numerator and a denominator, prints the fractional value in a special format. eg. Numerator: 1, Denominator: 3. So Num/Denom = 0.3333333333, but output should be .(3) similarly 0.123123 as .(123) also 0.34232323 as 0.34(23)
6.13. Given a string construct a new string containing the occurrences of unique characters in it. You can
assume that only a-z & A-Z appear in the string with ‘a’ being different from ‘A’. Also the letters in the output string must be in the order of their occurrence in the input string.
e.g. for the string “ThunderBirdFirefox” you must return the string “T1h1u1n1d2e2r3B1i2F1f1o1x1″
6.14. Given an integer array of which both first half and second half are sorted. Write a function to merge the two parts to create one single sorted array in place [do not use any extra space].
e.g. If input array is [1,3,6,8,-5,-2,3,8] It should be converted to: [-5,-2,1,3,3,6,8,8]
6.15. If the Fibonacci series is 1,2,3,5,8,13,….. then 10 can be written as 8 + 2 ==> 10010 and 17 can be written as 13 + 3 + 1 ==> 100101. Got it??
The Question was, given n, I need to get all possible representations of n in Fibonacci Binary Number System.
as 10 = 8 + 2 ==> 10010
also 10 = 5 + 3 + 2 ==> 1110
6.17. Given a string S of alphabets and 2 characters a,b find the minimum distance between instances of them such that position of a <= position of b.
5.5. Given an n X n array with rows sorted and cols sorted, find the number of negative elements in most efficient way.
5.7. Design a data structure with the following properties.
a. Insert in O(logn)
b. Delete in O(logn)
c. FindMin in O(logn)
d. FindMax in O(logn)
5.8. How will you find the number of all possible common subsequences between 2 given strings.
4.6. Suppose the positive integer n is odd. I write the numbers 1, 2, 3, …, 2n on the blackboard. Then I pick two numbers a, b, erase them and write, instead, |a-b|. Continue this practice till a single number remain on the board. Prove that an odd number will remain at the end.
4.7. Given an array with some repeating numbers. Like 12,6,5,12,6.
output should be printed as 12,12,6,6,5; i.e, all occurrences of the no. should come together and nos. should come in the order given in the array.
3.1. Given and Array of A[1..n] bits, find the length of longest consecutive substring of 0 and 1 such that number of 0s and 1s are equal.
3.3. In a 1D rotated (about an unknown index) sorted array, an element is local minimum if a[i-1] < a[i] < a[i+1]. How to find an index where local minimum exists in O(logn). Similar question in a 2D array where a local minimum is the number which is smaller than all its 4 neighbors.
3.5. You are given a character array say "aabcdadabc"-you have to generate all unique patterns(subarrays) containing 2 or more chars? Minimize time and space complexity.
3.6. Given an array of 'n' random integers. Given an integer 1<=n. Find the k numbers such that the minimum difference of all the possible pairs of k numbers is maximum (maximum among other minimum differences for various possible selections of k numbers ).
2.1. You are given 2 arrays of size ‘n’ each. You need to stable-merge these arrays such that in the new array sum of product of consecutive elements is maximized.
e.g.
A= { 2, 1, 3}
B= { 3, 7, 9}
Stable merging A and B will give an array C with ’2n’ elements say C={c1, c2, c3, c4, c5, c6}
You need to find a new array C by merging (stable) A and B such that sum= c1*c2 + c3*c4 + c5* c6….. n terms is maximum.
1.1. Consider a binary tree, and listing of its nodes in the inorder traversal. Now write a function such that given any node in the tree it returns the next node that is visited by in the inorder traversal program.
E.g. if the is tree is like root – 1 has children 2, 3. 2 has children 4, 5. Then given 4, it should return 2 and given 5 it should return 1 etc.