Google interview question: quickSort-like questions

上一篇总结了mergeSort-like questions,这篇总结一下有关quickSort的问题。

Question:

Given an array of object A, and an array of object B. All A‘s have 
different sizes, and all B‘s have different sizes. Any object A is of the 
same size as exactly one object B. We have a function f(A, B) to compare the 
size of one A and one B. But we cannot compare between two A‘s or two B‘s. 
Give an algorithm to match each A with each B.

Brute force的做法时间复杂度是O(N^2)。这个问题的本质是利用quickSort来进行matching,平均时间复杂度为O(Nlog(N))。因为相同的数组之间不能进行比较,所以需要在一个数组中选出一个元素,作为另一个数组的pivot进行划分,然后递归直到所有元素都一一对应。这个问题也叫做matching nuts & bolts。

public class MatchingNutsAndBolts {
    public static void main(String arcg[]){
        int[] nuts = {3,1,5,2,6,4};
        int[] bolts = {5,1,2,6,4,3};
        matchPairs(nuts,bolts,0,nuts.length-1);
        System.out.println(Arrays.toString(nuts));
        System.out.println(Arrays.toString(bolts));
    }
    public static void matchPairs(int[] nuts, int[] bolts, int low, int high){
        if(low < high){
            int pivot = partition(nuts, low, high, bolts[low]);
            partition(bolts, low, high, nuts[pivot]);
            matchPairs(nuts, bolts, low, pivot-1);
            matchPairs(nuts, bolts, pivot+1, high);
        }
    }
    public static int partition(int[] array, int low, int high, int pivot){
        int i=low,j = high;
        while(i<=j){
            if(array[i] > pivot){
                swap(array, i, j);
                j--;
            }
            else if(array[i] < pivot){
                i++;
            }
            else{
                swap(array, low, i);
                i++;
            }
        }
        swap(array, low, i-1);
        return i-1;
    }
    public static void swap(int[] array, int i, int j){
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }
}
时间: 2024-10-27 19:55:20

Google interview question: quickSort-like questions的相关文章

Google interview question: disjoint-set questions

Question: Given a n,m which means the row and column of the 2D matrix and an array of pair A( size k). Originally, the 2D matrix is all 0 which means there is only sea in the matrix. The list pair has k operator and each operator has two integer A[i]

Google interview question: k-nearest neighbor (k-d tree)

Question: You are given information about hotels in a country/city. X and Y coordinates of each hotel are known. You need to suggest the list of nearest hotels to a user who is querying from a particular point (X and Y coordinates of the user are giv

Google interview question: count bounded slices(min/max queue)

Question: A Slice of an array said to be a Bounded slice if Max(SliceArray)-Min(SliceArray)<=K. If Array [3,5,6,7,3] and K=2 provided .. the number of bounded slice is 9, first slice (0,0) in the array Min(0,0)=3 Max(0,0)=3 Max-Min<=K result 0<=2

Garena interview question

Web Developer Interview Anonymous Interview Candidate in Singapore (Singapore) No Offer Neutral Experience Difficult Interview Application I applied through an employee referral. The process took 5 days. I interviewed at Garena Online (Singapore (Sin

Interview Question

HDS(11.16.2015): How to design an non-stop website like Google or Amazon? What design patterns are you using? Implement a queue with two stacks. Quick-sort partition on a single linked list.

Interview Question Overload、Refactoring和Override?

Overload Overload我们百度翻译知道是超载的意思,不过我们一般称其为重载,在这里我们不纠结于它的翻译,我们来讲讲重载是什么意思,重载的好处.在下面我们以Overload来代表重载(为了记英语单词,你懂得). Overload是什么 Overload:顾名思义,就是Over(重新)--load(加载),所以中文名称是重载.它可以表现类的多态性,可以是函数里面可以有相同的函数名但是参数名.返回值.类型不能相同:或者说可以改变参数.类型.返回值但是函数名字依然不变. Overload的好

2-D range sum query implementation(2-D segment tree)

Google interview question:一个二维数组,有两个方法,一个是update(x,y),更新一个cell的值,一个是query(x1,y1,x2,y2),查询(x1,y1,x2,y2)矩形内所有元素的和. Senario 1. update调用次数远大于query. Senario 2. query调用次数远大于update. Senario 3. 两种方法调用一样多. 对于senario 1,只需要用一个二维数组储存数据,update相应的cell,时间复杂度O(1),qu

Top 10 tough core Java interview questions answers programming

Tough core Java interview questions and answersWhat is tough core java interview question ? Why do people look for tough Java questions before going for interview? well I don't thing I need to answer these tough questions because its pretty natural t

Telephone interview with Youyou Tu

"Good News for the National Holiday!" Telephone interview with Youyou Tu following the announcement of the 2015 Nobel Prize in Physiology or Medicine, 5 October 2015. The interviewer is Adam Smith, Chief Scientific Officer of Nobel Media. Profes