Sure interview all-1103

101
SureInterview PREPARATION http://www.sureinterview.com Mar 6, 2011 The latest version can be found at http://code.google.com/p/sureinterview/downloads

description

 

Transcript of Sure interview all-1103

  • 1. SureInterview PREPARATION http://www.sureinterview.com Mar 6, 2011 The latest version can be found at http://code.google.com/p/sureinterview/downloads
  • 2. Application SearchDesign data structure and algorithm for interactive spell checker, should provides correct candidates.Design data structure and algorithm for interactive spell checker, should provides correct candidates.Check how to write a spelling corrector, and how to improve performance of generating potential candidates. Searchmillions of book, how to find duplicates(book title may have error)millions of book, how to find duplicates(book title may have error)submit my answer
  • 3. SearchIn which case O(n^2) is better than O(nlgn)?In which case O(n2) is better than O(nlgn)?Consider following aspects: saving space more function simpler code
  • 4. Dynamic programming SearchLongest Common SubsequenceFinding the longest common subsequence of two sequences of items.For example, the longest common subsequence of following two sequences is ACAAA.A b C d A A AA C e A f A ACheck longest common subsequence (@algorithmist) for some general description, and Dynamic programming and sequencealignment (@IBM) for detail explanation and examples.As a quick recap, a m*n matrix is used to bookmark the current alignment result. A i,j is calculated from previous 3 adjacentalignment result, with different score/penalty considered.There are two steps in the longest common subsequence (or, alignment algorithm). 1. Find the length or score of the longest common subsequence. That is, calculate from A 0,0 to A m-1,n-1. 2. Trace back from A m-1,n-1 to A 0,0 to find the exact alignment. SearchGiven a n stair case of n levels, step up one or two levels at a time. 1. How many ways to get to 10th stair? 2. Print all the possible path? 3. Only {1,3,5} levels are allowed?submit my answer SearchKnapsack problemGiven some items, pack the knapsack to get the maximum total value. Each item has some weight and some value. Total weightthat we can carry is no more than some fixed number W.This slide [1] illustrates the idea of dynamic programming using knapsack problem as an example.The similar idea applies to more general combination problems. Please check out the way combination is progressively calculatedin Yang_Huis_Triangle.References 1. Dynamic Programming
  • 5. SearchHow many different binary trees with n nodes ?Different topology are counted as different. For example, following two trees are treated as different.Tree 1: o /oTree 2:o osubmit my answer Searchsub-matrix sumGiven a matrix of integers. How to calculate the sum of a sub-matrix. A sub-matrix is represented by x, y, h, w, where x and yis the position of the upper-left corner, h is the height, w is the width.int[][] matrix int sum(int x, int y, int h, int w){...}2. if this function is called frequently on the same matrix, how to optimise it?origsubmit my answer Searchviewable blocksYou are given N blocks of height 1..N. In how many ways can you arrange these blocks in a row such that when viewed fromleft you see only L blocks (rest are hidden by taller blocks) and when seen from right you see only R blocks?Example given N=3, L=2, R=1 there is only one arrangement {2, 1, 3} while for N=3, L=2, R=2 there are two ways {1, 3, 2}and {2, 3, 1}.General IdeaReduce the size of the problemBy examine the test cases, we know the size of the problem can be reduced by fixing the smallest element.Suppose the number of combination is F(N, L, R). If the shortest block is on left most position, there are F(N-1, L-1, R) ways ofcombination. Similarly, right most position gives F(N-1, L, R-1) ways of combination. Taking out this shortest block, it will beF(N-1, L, R). And there is N-2 positions to put this shortest block back. So, we haveF(N, L, R) = F(N-1, L-1, R) + F(N-1, L, R-1) + (N-2)*F(N-1, L, R)Divide the problemThe tallest block divides the blocks into two parts. Both the left part and right parts can be calculated independently in a similarfashion.Now, Consider a simplified problem that we only look at the blocks from left. Using the similar logic in both-side version, wehave:G(N, L) = G(N-1, L-1) + (N-1)*G(N-1, L)Back to the original problem, if the tallest block is on the left most position, we have:F(N-1, 1, R-1) = G(N-1, R-1)If it is on position i ( 2