Given a random number generator which can generate the number in range (1,5) uniformly. How can you use it to build a random number generator which can generate the number in range (1,7) uniformly?
void generate_random_m_from_n(int n, int m) { int i, remaining, select = m; srand(time(NULL)); for(i=0; i<n; i++) { remaining = n - i; if(rand()%remaining < select) { printf("%d\t", i); --select; } } printf("\n"); }
int Shuffle(int[] a, int len)
{
for (int i = len - 1; i > 0; i--)
{
// Select an element from index 0 to i randomly;
int index = GetRandomNumber(0, i);
// exchange a[i] with a[index]
Swap(a[index], a[i]);
}
}
欏轟究涔熻創鍑簆ython鐨剅andom鍗曞厓鍏充簬shuffle鐨勫疄鐜幫細
def shuffle(self, x, random=None, int=int):
"""x, random=random.random -> shuffle list x in place; return None.
Optional arg random is a 0-argument function returning a random
float in [0.0, 1.0); by default, the standard random.random.
"""
if random is None:
random = self.random
for i in reversed(xrange(1, len(x))):
# pick an element in x[:i+1] with which to exchange x[i]
j = int(random() * (i+1))
x[i], x[j] = x[j], x[i]
Why are manhole covers round? 錛堥檲鐨擄細涓轟粈涔堜笅姘翠簳鐩栨槸鍦嗙殑錛熻繖鏄湁N縐嶇瓟妗堢殑錛屼笂Wiki鐪嬬湅鍚э級
What is the difference between a mutex and a semaphore? Which one would you use to protect access to an increment operation?
A man pushed his car to a hotel and lost his fortune. What happened? 錛堥檲鐨擄細鑴戠瓔鎬ヨ漿寮紵浠栧湪鐜╁ぇ瀵岀縼娓告垙錛燂紒錛侊級
Explain the significance of “dead beef”.錛堥檲鐨擄細瑕佹槸浣犵湅鍒扮殑鏄?6榪涘埗 DEAD BEEF錛屼綘浼氳寰楄繖鏄粈涔堬紵IPv6鐨勫湴鍧錛燂級
Write a C program which measures the the speed of a context switch on a UNIX/Linux system.
Given a function which produces a random integer in the range 1 to 5, write a function which produces a random integer in the range 1 to 7.錛堥檲鐨擄細涓奡tackOverflow鐪嬬湅鍚э紝緇忓吀鐨勯棶棰橈級
Describe the algorithm for a depth-first graph traversal.
Design a class library for writing card games. 錛堥檲鐨擄細鐢ㄤ竴緋誨垪鐨勭被鏉ヨ璁′竴涓墤鍏嬫父鎴忥紝璁捐棰橈級
You need to check that your friend, Bob, has your correct phone number, but you cannot ask him directly. You must write a the question on a card which and give it to Eve who will take the card to Bob and return the answer to you. What must you write on the card, besides the question, to ensure Bob can encode the message so that Eve cannot read your phone number?錛堥檲鐨擄細鍗忚+鏁板瓧鍔犲瘑錛屾垜璇曟兂浜嗕竴涓紝綰告潯涓婂彲浠ヨ繖鏍峰啓錛?#8220;Bob錛岃鎶婃垜鐨勬墜鏈哄彿浠D5綆楁硶鍔犲瘑鍚庣殑瀛楃涓詫紝姣斿涓嬮潰鐨勫瓧絎︿覆——XXXXXX錛屽畠浠槸涓鏍風殑鍚楋紵”錛?/li>
How are cookies passed in the HTTP protocol?
Design the SQL database tables for a car rental database.
Write a regular expression which matches a email address. 錛堥檲鐨擄細涓奡tackOverflow鏌ョ浉褰撶殑闂鍚с傦級
Write a function f(a, b) which takes two character string arguments and returns a string containing only the characters found in both strings in the order of a. Write a version which is order N-squared and one which is order N.錛堥檲鐨擄細綆楁硶棰橈紝涓嶉毦錛屼笉璇翠簡銆備竴涓狾(n^2)鍜屼竴涓狾(n)鐨勭畻娉曞鏉傚害錛?/li>
You are given a the source to a application which is crashing when run. After running it 10 times in a debugger, you find it never crashes in the same place. The application is single threaded, and uses only the C standard library. What programming errors could be causing this crash? How would you test each one? 錛堥檲鐨擄細鍜岄殢鏈烘暟鏈夊叧緋伙紵鎴栨槸鏃墮棿錛燂級
Explain how congestion control works in the TCP protocol.
In Java, what is the difference between final, finally, and finalize?
What is multithreaded programming? What is a deadlock?
Write a function (with helper functions if needed) called to Excel that takes an excel column value (A,B,C,D…AA,AB,AC,… AAA..) and returns a corresponding integer value (A=1,B=2,… AA=26..).
You have a stream of infinite queries (ie: real time Google search queries that people are entering). Describe how you would go about finding a good estimate of 1000 samples from this never ending set of data and then write code for it.
Tree search algorithms. Write BFS and DFS code, explain run time and space requirements. Modify the code to handle trees with weighted edges and loops with BFS and DFS, make the code print out path to goal state.
You are given a list of numbers. When you reach the end of the list you will come back to the beginning of the list (a circular list). Write the most efficient algorithm to find the minimum # in this list. Find any given # in the list. The numbers in the list are always increasing but you don’t know where the circular list begins, ie: 38, 40, 55, 89, 6, 13, 20, 23, 36. 錛堥檲鐨擄細寰幆鎺掑簭鏁扮粍鐨勪簩鍒嗘煡鎵鵑棶棰橈級
Describe the data structure that is used to manage memory. (stack)
What’s the difference between local and global variables?
If you have 1 million integers, how would you sort them efficiently? (modify a specific sorting algorithm to solve this)
In Java, what is the difference between static, final, and const. (if you don’t know Java they will ask something similar for C or C++).
Talk about your class projects or work projects (pick something easy)… then describe how you could make them more efficient (in terms of algorithms).
Suppose you have an NxN matrix of positive and negative integers. Write some code that finds the sub-matrix with the maximum sum of its elements.錛堥檲鐨擄細浠ュ墠瑙佽繃涓緇存暟緇勭殑榪欎釜闂錛岀幇鍦ㄦ槸浜岀淮鐨勩傛劅瑙夊簲璇ユ槸鎶婁簩緇寸殑絎竴琛岀殑鏈澶у拰鐨勫尯闂寸畻鍑烘潵錛岀劧鍚庡啀鍦ㄨ繖涓熀紜涔嬩笂榪涜浜岀淮鐨勫垎鏋愩傛濊礬搴旇鏄繖涓紝涓嶈繃鍏蜂綋鐨勭畻娉曡繕闇瑕佹兂涓鎯籌級
Write some code to reverse a string.
Implement division (without using the divide operator, obviously).錛堥檲鐨擄細鎯充竴鎯蟲墜綆楅櫎娉曠殑榪囩▼銆傦級
Write some code to find all permutations of the letters in a particular string.
What method would you use to look up a word in a dictionary? 錛堥檲鐨擄細浣跨敤鎺掑簭錛屽搱甯岋紝鏍戠瓑綆楁硶鍜屾暟鎹粨鏋勶級
Imagine you have a closet full of shirts. It’s very hard to find a shirt. So what can you do to organize your shirts for easy retrieval?
You have eight balls all of the same size. 7 of them weigh the same, and one of them weighs slightly more. How can you fine the ball that is heavier by using a balance and only two weighings?
What is the C-language command for opening a connection with a foreign host over the internet?
Design and describe a system/application that will most efficiently produce a report of the top 1 million Google search requests. These are the particulars: 1) You are given 12 servers to work with. They are all dual-processor machines with 4Gb of RAM, 4x400GB hard drives and networked together.(Basically, nothing more than high-end PC’s) 2) The log data has already been cleaned for you. It consists of 100 Billion log lines, broken down into 12 320 GB files of 40-byte search terms per line. 3) You can use only custom written applications or available free open-source software.
There is an array A[N] of N numbers. You have to compose an array Output[N] such that Output[i] will be equal to multiplication of all the elements of A[N] except A[i]. For example Output[0] will be multiplication of A[1] to A[N-1] and Output[1] will be multiplication of A[0] and from A[2] to A[N-1]. Solve it without division operator and in O(n).錛堥檲鐨擄細娉ㄦ剰鍏朵笉鑳戒嬌鐢ㄩ櫎娉曘傜畻娉曟濊礬鏄繖鏍風殑錛屾妸output[i]=a[i]宸﹁竟鐨勪箻縐?x a[i]鍙寵竟鐨勪箻縐紝鎵浠ワ紝鎴戜滑鍙互鍒嗕袱涓驚鐜紝絎竴嬈″厛鎶夾[i]宸﹁竟鐨勪箻縐斁鍦∣utput[i]涓紝絎簩嬈℃妸A[i]鍙寵竟鐨勪箻縐畻鍑烘潵銆傛垜浠厛鐪嬬涓嬈$殑寰幆錛屼嬌鐢ㄨ凱浠g瘡縐殑鏂瑰紡錛屼唬鐮佸涓嬶細for(r=1; i=0; i<n-1; i++){ Output[i]=r; r*=a[i]; }錛岀湅鏄庣櫧浜嗗惂銆傜浜屾鐨勫驚鐜垜灝變笉璇翠簡錛屾柟娉曚竴鏍風殑銆傦級
There is a linked list of numbers of length N. N is very large and you don’t know N. You have to write a function that will return k random numbers from the list. Numbers should be completely random. Hint: 1. Use random function rand() (returns a number between 0 and 1) and irand() (return either 0 or 1) 2. It should be done in O(n).錛堥檲鐨擄細鏈鍏跺疄涓嶉毦銆傚湪閬嶅巻閾捐〃鐨勫悓鏃朵竴杈圭敓鎴愰殢鏈烘暟錛屼竴杈硅褰曟渶澶х殑K涓殢鏈烘暟鍜屽叾閾炬帴鍦板潃銆傦級
Find or determine non existence of a number in a sorted list of N numbers where the numbers range over M, M>> N and N large enough to span multiple disks. Algorithm to beat O(log n) bonus points for constant time algorithm.錛堥檲鐨擄細浣跨敤bitmap錛屽鏋滀竴涓暱鏁村艦鏈?4浣嶏紝閭d箞鎴戜滑鍙互浣跨敤M/64涓猙itmap錛?/li>
You are given a game of Tic Tac Toe. You have to write a function in which you pass the whole game and name of a player. The function will return whether the player has won the game or not. First you to decide which data structure you will use for the game. You need to tell the algorithm first and then need to write the code. Note: Some position may be blank in the game啷?So your data structure should consider this condition also.
You are given an array [a1 To an] and we have to construct another array [b1 To bn] where bi = a1*a2*…*an/ai. you are allowed to use only constant space and the time complexity is O(n). No divisions are allowed.錛堥檲鐨擄細鍓嶉潰璇磋繃浜嗭級
How do you put a Binary Search Tree in an array in a efficient manner. Hint :: If the node is stored at the ith position and its children are at 2i and 2i+1(I mean level order wise)Its not the most efficient way.錛堥檲鐨擄細鎸夐『搴忛亶鍘嗘爲錛?/li>
How do you find out the fifth maximum element in an Binary Search Tree in efficient manner. Note: You should not use use any extra space. i.e sorting Binary Search Tree and storing the results in an array and listing out the fifth element.
Given a Data Structure having first n integers and next n chars. A = i1 i2 i3 … iN c1 c2 c3 … cN.Write an in-place algorithm to rearrange the elements of the array ass A = i1 c1 i2 c2 … in cn錛堥檲鐨擄細榪欎釜綆楁硶鍏跺疄灝辨槸浠庝腑闂村紑濮嬩氦鎹㈠厓绱狅紝浠g爜錛歠or(i=n-1; i>1; i++) { for(j=i; j<2*n-i; j+=2) { swap(a[j], a[j+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.
Given That One of the strings is very very long , and the other one could be of various sizes. Windowing will result in O(N+M) solution but could it be better? May be NlogM or even better?
How many lines can be drawn in a 2D plane such that they are equidistant from 3 non-collinear points?
Let’s say you have to construct Google maps from scratch and guide a person standing on Gateway of India (Mumbai) to India Gate(Delhi). How do you do the same?
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?
Given a binary tree, programmatically you need to prove it is a binary search tree.
You are given a small sorted list of numbers, and a very very long sorted list of numbers – so long that it had to be put on a disk in different blocks. How would you find those short list numbers in the bigger one?
Suppose you have given N companies, and we want to eventually merge them into one big company. How many ways are theres to merge?
Given a file of 4 billion 32-bit integers, how to find one that appears at least twice? 錛堥檲鐨擄細鎴戣兘鎯沖埌鐨勬槸鎷嗗垎鎴愯嫢騫蹭釜灝忔暟緇勶紝鎺掑簭錛岀劧鍚庝竴鐐圭偣褰掑茍璧鋒潵錛?/li>
Write a program for displaying the ten most frequent words in a file such that your program should be efficient in all complexity measures.錛堥檲鐨擄細浣犲彲鑳介渶瑕佺湅鐪嬭繖綃囨枃绔?a target="_blank">Finding Frequent Items in Data Streams錛?/li>
Design a stack. We want to push, pop, and also, retrieve the minimum element in constant time.
Given a set of coin denominators, find the minimum number of coins to give a certain amount of change.錛堥檲鐨擄細浣犲簲璇ユ煡鐪嬩竴涓嬭繖綃囨枃绔狅細Coin Change Problem錛?/li>
Given an array, i) find the longest continuous increasing subsequence. ii) find the longest increasing subsequence.錛堥檲鐨擄細榪欎釜棰樹笉闅撅紝O(n)綆楁硶鏄竟閬嶅巻杈硅褰曞綋鍓嶆渶澶х殑榪炵畫鐨勯暱搴︺傦級
Suppose we have N companies, and we want to eventually merge them into one big company. How many ways are there to merge?
Write a function to find the middle node of a single link list. 錛堥檲鐨擄細鎴戣兘鎯沖埌鐨勭畻娉曟槸——璁劇疆涓や釜鎸囬拡p1鍜宲2錛屾瘡涓嬈★紝p1璧頒袱姝ワ紝p2璧頒竴姝ワ紝榪欐牱錛屽綋p1璧板埌鏈鍚庢椂錛宲2灝卞湪涓棿錛?/li>
Given two binary trees, write a compare function to check if they are equal or not. Being equal means that they have the same value and same structure.錛堥檲鐨擄細榪欎釜寰堢畝鍗曪紝浣跨敤閫掑綊綆楁硶銆傦級
Implement put/get methods of a fixed size cache with LRU replacement algorithm.
You are given with three sorted arrays ( in ascending order), you are required to find a triplet ( one element from each array) such that distance is minimum. Distance is defined like this : If a[i], b[j] and c[k] are three elements then distance=max(abs(a[i]-b[j]),abs(a[i]-c[k]),abs(b[j]-c[k]))” Please give a solution in O(n) time complexity錛堥檲鐨擄細涓変釜鎸囬拡錛宎, b, c鍒嗗埆鎸囧悜涓変釜鏁扮粍澶達紝鍋囪錛歛[0]<b[0]<c[0]錛屾帹榪沘鐩村埌a[i]>b[0]錛岃綆?abs(a[i-1] – c[0])錛屾妸緇撴灉淇濆瓨鍦╩in涓傜幇鍦ㄦ儏鍐靛彉鎴愭壘 a[i], b[0],c[0]錛岄噸澶嶄笂榪拌繃紼嬶紝濡傛灉鏈変竴涓柊鐨勫兼瘮min瑕佸皬錛岄偅灝卞彇浠g幇鏈夌殑min銆傦級
How does C++ deal with constructors and deconstructors of a class and its child class?
Write a function that flips the bits inside a byte (either in C++ or Java). Write an algorithm that take a list of n words, and an integer m, and retrieves the mth most frequent word in that list.
What’s 2 to the power of 64?
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? 錛堥檲鐨擄細鎴戣兘鎯沖埌鐨勬槸——鎶婇偅M涓皬瀛椾覆鎺掍釜搴忥紝鐒跺悗閬嶅巻澶у瓧涓詫紝騫跺湪閭涓瓧涓蹭腑浠ヤ簩鍒嗗彇涓殑鏂瑰紡鏌ユ壘銆傦級
How do you find out the fifth maximum element in an Binary Search Tree in efficient manner.
Suppose we have N companies, and we want to eventually merge them into one big company. How many ways are there to merge?
There is linked list of millions of node and you do not know the length of it. Write a function which will return a random number from the list.
You need to check that your friend, Bob, has your correct phone number, but you cannot ask him directly. You must write a the question on a card which and give it to Eve who will take the card to Bob and return the answer to you. What must you write on the card, besides the question, to ensure Bob can encode the message so that Eve cannot read your phone number?
How long it would take to sort 1 trillion numbers? Come up with a good estimate.
Order the functions in order of their asymptotic performance: 1) 2^n 2) n^100 3) n! 4) n^n
There are some data represented by(x,y,z). Now we want to find the Kth least data. We say (x1, y1, z1) > (x2, y2, z2) when value(x1, y1, z1) > value(x2, y2, z2) where value(x,y,z) = (2^x)*(3^y)*(5^z). Now we can not get it by calculating value(x,y,z) or through other indirect calculations as lg(value(x,y,z)). How to solve it?
How many degrees are there in the angle between the hour and minute hands of a clock when the time is a quarter past three?
Given an array whose elements are sorted, return the index of a the first occurrence of a specific integer. Do this in sub-linear time. I.e. do not just go through each element searching for that element.
Given two linked lists, return the intersection of the two lists: i.e. return a list containing only the elements that occur in both of the input lists. 錛堥檲鐨擄細鎶婄涓涓摼琛ㄥ瓨鍏ash琛紝鐒跺悗閬嶅巻絎簩涓摼琛ㄣ備笉鐭ラ亾榪樻病鏈夋洿濂界殑鏂規硶銆傦級
What’s the difference between a hashtable and a hashmap?
If a person dials a sequence of numbers on the telephone, what possible words/strings can be formed from the letters associated with those numbers?錛堥檲鐨擄細榪欎釜闂鍜岀編鍥界殑鐢佃瘽鏈夊叧緋伙紝澶у鍙互璇曠潃鎯充竴涓嬫垜浠彂鐭俊鐨勬墜鏈猴紝鎸夋暟瀛楅敭鍑哄瓧姣嶏紝涓涓粍鍚堢殑鏁板闂銆傦級
How would you reverse the image on an n by n matrix where each pixel is represented by a bit?
Create a fast cached storage mechanism that, given a limitation on the amount of cache memory, will ensure that only the least recently used items are discarded when the cache memory is reached when inserting a new item. It supports 2 functions: String get(T t) and void put(String k, T t).
Create a cost model that allows Google to make purchasing decisions on to compare the cost of purchasing more RAM memory for their servers vs. buying more disk space.
Design an algorithm to play a game of Frogger and then code the solution. The object of the game is to direct a frog to avoid cars while crossing a busy road. You may represent a road lane via an array. Generalize the solution for an N-lane road.
What sort would you use if you had a large data set on disk and a small amount of ram to work with?
What sort would you use if you required tight max time bounds and wanted highly regular performance.
How would you store 1 million phone numbers?錛堥檲鐨擄細璇曟兂鐢佃瘽鏄湁鍖烘鐨勶紝鍙互鎶婂尯孌電粺涓淇濆瓨錛孎lyweight璁捐妯″紡錛?/li>
Design a 2D dungeon crawling game. It must allow for various items in the maze – walls, objects, and computer-controlled characters. (The focus was on the class structures, and how to optimize the experience for the user as s/he travels through the dungeon.)
What is the size of the C structure below on a 32-bit system? On a 64-bit? 錛堥檲鐨擄細娉ㄦ剰緙栬瘧鍣ㄧ殑瀵歸綈錛?
絳旀錛?/span>鍏堝悗鎵撳嵃鍑轟袱琛?/span>:A is constructed. B is constructed. 璋冪敤B鐨勬瀯閫犲嚱鏁版椂錛屽厛浼氳皟鐢?/span>B鐨勫熀綾誨強A鐨勬瀯閫犲嚱鏁般傜劧鍚庡湪A鐨勬瀯閫犲嚱鏁伴噷璋冪敤Print銆傜敱浜庢鏃跺疄渚嬬殑綾誨瀷B鐨勯儴鍒嗚繕娌℃湁鏋勯犲ソ錛屾湰璐ㄤ笂瀹冨彧鏄?/span>A鐨勪竴涓疄渚嬶紝浠栫殑铏氬嚱鏁拌〃鎸囬拡鎸囧悜鐨勬槸綾誨瀷A鐨勮櫄鍑芥暟琛ㄣ傚洜姝ゆ鏃惰皟鐢ㄧ殑Print鏄?/span>A::Print錛岃屼笉鏄?/span>B::Print銆傛帴鐫璋冪敤綾誨瀷B鐨勬瀯閫犲嚱鏁幫紝騫惰皟鐢?/span>Print銆傛鏃跺凡緇忓紑濮嬫瀯閫?/span>B錛屽洜姝ゆ鏃惰皟鐢ㄧ殑Print鏄?/span>B::Print銆?/span>
///////////////////////////////////////////////////////////////////////// // Find continuous sequence, whose sum is n ///////////////////////////////////////////////////////////////////////// void FindContinuousSequence(int n) { if(n < 3) return;
int small = 1; int big = 2; int middle = (1 + n) / 2; int sum = small + big;
while(small < middle) { // we are lucky and find the sequence if(sum == n) PrintContinuousSequence(small, big);
// if the current sum is greater than n, // move small forward while(sum > n) { sum -= small; small ++;
// we are lucky and find the sequence if(sum == n) PrintContinuousSequence(small, big); }
// move big forward big ++; sum += big; } }
///////////////////////////////////////////////////////////////////////// // Print continuous sequence between small and big ///////////////////////////////////////////////////////////////////////// void PrintContinuousSequence(int small, int big) { for(int i = small; i <= big; ++ i) printf("%d ", i);
int StackWithMin::pop() { if(top_index ==-1) { printf("pop() failed: Stack Empty\n"); return-1; } int ret = stack[top_index]; if(min_index == top_index) min_index = index[top_index]; --top_index; return ret; }
class StackWithMin { public: StackWithMin(); ~StackWithMin(); int get_min() const; int top() const; void push(int value); int pop(); private: static const int MAX_SIZE = 101; int top_index, min_index; int stack[MAX_SIZE]; int index[MAX_SIZE]; };
/* Problem: Convert a binary search tree into a sorted linkedlist */ /* When it comes to Tree-Structure, recursion is always the most common solution. When designing recursion solution, should consider: 1. the parameters 2(important). the return object */
/* Convert the left tree into a sorted linkedlist */ struct Node *l_linkedlist = BTree2List(root->left); ret = l_linkedlist==NULL ? root : l_linkedlist;
/* Convert the right tree into a sorted linkedlist */ struct Node *r_linkedlist = BTree2List(root->right); while(l_linkedlist && l_linkedlist->right) l_linkedlist = l_linkedlist->right;