InterviewQ Algorithm

download InterviewQ Algorithm

of 10

description

InterviewQ Algorithm

Transcript of InterviewQ Algorithm

Interview Questions: Union-FindThedue datefor this homework isMon 25 Mar 2013 8:59 PM PDT.Top of FormThese interview questions are for your own enrichment and are not assessed. If you click theSubmit Answersbutton, you will get a hint.Question 1Social network connectivity.Given a social network containingNmembers and a log file containingMtimestamps at which times pairs of members formed friendships, design an algorithm to determine the earliest time at which all members are connected (i.e., every member is a friend of a friend of a friend ... of a friend). Assume that the log file is sorted by timestamp and that friendship is an equivalence relation. The running time of your algorithm should beMlogNor better and use extra space proportional toN.Question 2Union-find with specific canonical element.Add a methodfind()to the union-find data type so thatfind(i)returns the largest element in the connected component containingi. The operations,union(),connected(), andfind()should all take logarithmic time or better.

For example, if one of the connected components is{1,2,6,9}, then thefind()method should return9for each of the four elements in the connected components.Question 3Successor with delete.Given a set ofNintegersS={0,1,...,N1}and a sequence of requests of the following form: RemovexfromS Find thesuccessorofx: the smallestyinSsuch thatyx.design a data type so that all operations (except construction) should take logarithmic time or better.Question 4Union-by-size.Develop a union-find implementation that uses the same basic strategy as weighted quick-union but keeps track of tree height and always links the shorter tree to the taller one. Prove algNupper bound on the height of the trees forNsites with your algorithm.Bottom of Form

Interview Questions: Analysis of AlgorithmsThedue datefor this homework isMon 25 Mar 2013 8:59 PM PDT.Top of FormThese interview questions are for your own enrichment and are not assessed. If you click theSubmit Answersbutton, you will get a hint.Question 13-SUM in quadratic time.Design an algorithm for the 3-SUM problem that takes time proportional toN2in the worst case. You may assume that you can sort theNintegers in time proportional toN2or better.Question 2Search in a bitonic array.An array isbitonicif it is comprised of an increasing sequence of integers followed immediately by a decreasing sequence of integers. Write a program that, given a bitonic array ofNdistinct integer values, determines whether a given integer is in the array. Standard version: Use3lgNcompares in the worst case. Signing bonus: Use2lgNcompares in the worst case (and prove that no algorithm can guarantee to perform fewer than2lgNcompares in the worst case).Question 3Egg drop.Suppose that you have anN-story building and plenty of eggs. An egg breaks if it is dropped from floorTor higher and does not break otherwise. Your goal is to devise a strategy to determine the value ofTgiven the following limitations on the number of eggs and tosses: Version 0: 1 egg,Ttosses. Version 1:1lgNeggs and1lgNtosses. Version 2:lgTeggs and2lgTtosses. Version 3:2eggs and2Ntosses. Version 4:2eggs andcTtosses for some fixed constantc.Bottom of Form

Interview Questions: Stacks and QueuesThedue datefor this homework isMon 25 Mar 2013 8:59 PM PDT.Top of FormThese interview questions are for your own enrichment and are not assessed. If you click theSubmit Answersbutton, you will get a hint.Question 1Queue with two stacks.Implement a queue with two stacks so that each queue operations takes a constant amortized number of stack operations.Question 2Stack with max.Create a data structure that efficiently supports the stack operations (push and pop) and also a return-the-maximum operation. Assume the elements are reals numbers so that you can compare them.Question 3Java generics.Explain why Java prohibits generic array creation.Question 4Detect cycle in a linked list.A singly-linked data structure is a data structure made up of nodes where each node has a pointer to the next node (or a pointer to null). Suppose that you have a pointer to the first node of a singly-linked list data structure: Determine whether a singly-linked data structure contains a cycle. You may use only two pointers into the list (and no other variables). The running time of your algorithm should be linear in the number of nodes in the data structure. If a singly-linked data structure contains a cycle, determine the first node that participates in the cycle. you may use only a constant number of pointers into the list (and no other variables). The running time of your algorithm should be linear in the number of nodes in the data structure.You maynotmodify the structure of the linked list.Question 5Clone a linked structure with two pointers per node.Suppose that you are given a reference to the first node of a linked structure where each node has two pointers: one pointer to the next node in the sequence (as in a standard singly-linked list) and one pointer to an arbitrary node. private class Node { private String item; private Node next; private Node random; }Design a linear-time algorithm to create a copy of the doubly-linked structure. You may modify the original linked structure, but you must end up with two copies of the original.Bottom of Form

Interview Questions: Elementary SortsThedue datefor this homework isMon 25 Mar 2013 8:59 PM PDT.Top of FormThese interview questions are for your own enrichment and are not assessed. If you click theSubmit Answersbutton, you will get a hint.Question 1Intersection of two sets.Given two arraysa[]andb[], each containingNdistinct points in the plane, design a subquadratic algorithm to determine how many points are contained in both arrays.Question 2Permutation.Given two integer arrays of sizeN, design a subquadratic algorithm to determine whether one is a permutation of the other. That is, do they contain exactly the same entries but, possibly, in a different order.Question 3Dutch national flag.Given an array ofNbuckets, each containing a red, white, or blue pebble, sort them by color. The allowed operations are: swap(i,j): swap the pebble in bucketiwith the pebble in bucketj. color(i): color of pebble in bucketi.The performance requirements are as follows: At mostNcalls tocolor(). At mostNcalls toswap(). Constant extra space.Bottom of Form

Interview Questions: MergesortThedue datefor this homework isMon 25 Mar 2013 8:59 PM PDT.Top of FormThese interview questions are for your own enrichment and are not assessed. If you click theSubmit Answersbutton, you will get a hint.Question 1Merging with smaller auxiliary array.Suppose that the subarraya[0]toa[N-1]is sorted and the subarraya[N]toa[2*N-1]is sorted. How can you merge the two subarrays so thata[0]toa[2*N-1]is sorted using an auxiliary array of sizeN(instead of2N)?Question 2Counting inversions.Aninversionin an arraya[]is a pair of entriesa[i]anda[j]such thatia[j]. Given an array, design a linearithmic algorithm to count the number of inversions.Question 3Shuffling a linked list.Given a singly-linked list containingNitems, rearrange the items uniformly at random. Your algorithm should consume a logarithmic (or constant) amount of extra memory and run in time proportional toNlogNin the worst case.Bottom of Form

Interview Questions: QuicksortThedue datefor this homework isMon 25 Mar 2013 8:59 PM PDT.Top of FormThese interview questions are for your own enrichment and are not assessed. If you click theSubmit Answersbutton, you will get a hint.Question 1Nuts and bolts.A disorganized carpenter has a mixed pile ofNnuts andNbolts. The goal is to find the corresponding pairs of nuts and bolts. Each nut fits exactly one bolt and each bolt fits exactly one nut. By fitting a nut and a bolt together, the carpenter can see which one is bigger (but the carpenter cannot compare two nuts or two bolts directly). Design an algorithm for the problem that usesNlogNcompares (probabilistically).Question 2Selection in two sorted arrays.Given two sorted arraysa[]andb[], of sizesN1andN2, respectively, design an algorithm to find thekthlargest key. The order of growth of the worst case running time of your algorithm should belogN, whereN=N1+N2. Version 1:N1=N2andk=N/2 Version 2:k=N/2 Version 3: no restrictionsQuestion 3Decimal dominants.Given an array withNkeys, design an algorithm to find all values that occur more thanN/10times. The expected running time of your algorithm should be linear.Bottom of Form

Interview Questions: Priority QueuesThedue datefor this homework isMon 25 Mar 2013 8:59 PM PDT.Top of FormThese interview questions are for your own enrichment and are not assessed. If you click theSubmit Answersbutton, you will get a hint.Question 1Dynamic median.Design a data type that supports insert in logarithmic time, find-the-median in constant time, and remove-the-median in logarithmic time.Question 2Randomized priority queue.Describe how to add the methodssample()anddelRandom()to our binary heap implementation. The two methods return a key that is chosen uniformly at random among the remaining keys, with the latter method also removing that key. Thesample()method should take constant time; thedelRandom()method should take logarithmic time. Do not worry about resizing the underlying array.Question 3Taxicab numbers.Ataxicabnumber is an integer that can be expressed as the sum of two cubes of integers in two different ways:a3+b3=c3+d3. For example,1729=93+103=13+123. Design an algorithm to find all taxicab numbers witha,b,c, anddless thanN. Version 1:Use time proportional toN2logNand space proportional toN2. Version 2:Use time proportional toN2logNand space proportional toN.Bottom of Form

Interview Questions: Elementary Symbol TablesThedue datefor this homework isMon 25 Mar 2013 8:59 PM PDT.Top of FormThese interview questions are for your own enrichment and are not assessed. If you click theSubmit Answersbutton, you will get a hint.Question 1Java autoboxing and equals().Consider twodoublevaluesaandband their correspondingDoublevaluesxandy. Find values such that(a == b)istruebutx.equals(y)isfalse. Find values such that(a == b)isfalsebutx.equals(y)istrue.Question 2Check if a binary tree is a BST.Given a binary tree where eachNodecontains a key, determine whether it is a binary search tree. Use extra space proportional to the height of the tree.Question 3Inorder traversal with constant extra space.Design an algorithm to perform an inorder traversal of a binary search tree using only a constant amount of extra space.Question 4Web tracking.Suppose that you are trackingNweb sites andMusers and you want to support the following API: User visits a website. How many times has a given user visited a given site?What data structure or data structures would you use?Bottom of Form

Interview Questions: Balanced Search TreesThedue datefor this homework isMon 25 Mar 2013 8:59 PM PDT.Top of FormThese interview questions are for your own enrichment and are not assessed. If you click theSubmit Answersbutton, you will get a hint.Question 1Red-black BST with no extra memory.Describe how to save the memory for storing the color information when implementing a red-black BST.Question 2Document search.Design an algorithm that takes a sequence ofNdocument words and a sequence ofMquery words and find the shortest interval in which theMquery words appear in the document in the order given. The length of an interval is the number of words in that interval.Question 3Generalized queue.Design a generalized queue data type that supports all of the following operations in logarithmic time (or better) in the worst case. Create an empty data structure. Append an item to the end of the queue. Remove an item from the front of the queue. Return theithitem in the queue. Remove theithitem from the queue.Bottom of Form

Interview Questions: Hash TablesThedue datefor this homework isMon 25 Mar 2013 8:59 PM PDT.Top of FormThese interview questions are for your own enrichment and are not assessed. If you click theSubmit Answersbutton, you will get a hint.Question 14-SUM.Given an arraya[]ofNintegers, the 4-SUM problem is to determine if there exist distinct indicesi,j,k, andlsuch thata[i]+a[j]=a[k]+a[l]. Design an algorithm for the 4-SUM problem that takes time proportional toN2(under suitable technical assumptions).Question 2Hashing with wrong hashCode() or equals().Suppose that you implement a data typeOlympicAthletefor use in ajava.util.HashMap. Describe what happens if you overridehashCode()but notequals(). Describe what happens if you overrideequals()but nothashCode(). Describe what happens if you overridehashCode()but implementpublic boolean equals(OlympicAthlete that)instead ofpublic boolean equals(Object that).Bottom of Form