eBay interview summary

来源:互联网 发布:spring书籍推荐 知乎 编辑:程序博客网 时间:2024/06/07 10:01

Questions First Interviewer asked:

 

1.      Define a data structure ofstack to implement a findMin() function which will return the minimum of the stack. Time complexity should be O(1) and space complexityshould be O(n).

 

My answer:

Method 1: Use a variable to keep the min number. When pusha number into the stack, compare it with this variable, if it’s smaller thanthe variable then exchange these two numbers. The problem of this method isthat if the min number is popped out, then it can’t find another min number.

 

Method 2: Define two stacks and one is used to store allnumbers the other is used to store the min numbers. When push a number tostack1, push the current min number to stack2 and when pop a number fromstack1, pop a number from stack2 too. So the findMin() method only need toreturn the top of stack2.

This method meets the requirement and can still beoptimized in reducing the space complexity of stack2 and enhancing therobustness of code.

Actually this is a classical  interview question of MS and I happened to have seen it.


2.      Defined a data structure oralgorithm to evaluate 2-3*9/3-(4-6)/2。

 

My answer:

Method 1: Use two stacks and one is to store the numbersthe other is to store the operators. First it pushes 2 into stack1 and “-“ intostack2 and then pushes 3 into stack1, since the next operator is “*” which hashigher priority than the “-“, so I can evaluate 3*9, and next will do the samethings, compare the priority of operators before evaluate them.


Method 2: User a binary tree, store all the operators asthe root node of a tree and numbers as the left and right node of a tree andthen inorder traverse and evaluate it based on the priority of operators, sameway as method1.

 

Questions Second Interviewer asked:

 

1.      In ebay the sellers may have100 thousands items and to get all data from database is time consuming, so howoptimize the query from a large table?

 

My answer:

1.      I said pagination or cachingdata or using distributed data will help, but these technics are not allowed touse.

2.      Then I said I can split the bigtable into small tables, such as split it into ten tables and now I can queryform ten tables instead from one, which will speed up the query. Then thequestion is the query has a limitation in fetch size.

3.      To solve the problem of fetchsize, I said I can user multi thread technics, now there are ten tables andeach has 10 thousands records, assume the limitation fetch size of each queryis 1 thousand, then I can sent 10 threads to each table to execute the query,then I can get all records.

 

2.      Design a data structure thatoutputs the median of a stream of integers at any point in time.

 

My answer:

Method 1: I said this problem is based on finding themedian number of an array, so I’ll work on the array first and I designed amethod without sorting the array. Here I made a mistake is that if there aretwo median numbers then the real median number is the average of these twonumbers such as array 1,2,3,4 and the median number is 2.5.

 

Method 2: I redesigned the algorithm and sort the arrayfirst, so I can get the median number in a sorted array easily.

 

Method 3: To get the median number from an integer streamwithout sorting the data first and I designed the algorithm, my thought is:

(1)    if the data is the first data,then the median is itself.

(2)    If there are two data, themedian is the average of them.

(3)    If the new coming data will makethe whole data into odd, then compare it with the original two median data and thereal median is the smallest one.

(4)    If the new coming data willmake the whole data into even, then compare it wilh the original median and thesecond median number and the real median is the average of the two mediannumbers.

I send him mycode and he said it was not right, but I think my thought is right maybe I didn’texpress myself clearly.

 

3.      Given a dictionary of words anda string without spaces, output a string with space inserted appropriatelybetween words.

 

My answer:

I said this is a word segmentation problem based on adictionary and I was familiar with it since I had done thesame work in Chinese.

So my thought is:

(1)    Initialize the dictionary firstthen load them into an arraylist or hashmap.

(2)    Transform the input string intochar array.

(3)    Loop the char array and appendeach char into a StringBuffer and clean this StringBuffer  until that it is a word in the dictionary andthen do the same things.

 

When I was writing the code, he was making a call so I couldn’t communicate with him and Ijust implement this algorithm as what I thought and I knew this was the simplest way and not complete, but it was too late then he came back home.

 

Summary:

Maybe I was too urgent to answer the question and I need to think more and ask more before writing the code, and try to express myself as clearly as I can.


Question Third Interviewer Asked:

1. find more than half numbers in an array

2. find a missing number in an array

 

I knew how to solve these questions and I communicated a lot with him before I wrote the code. But there were some small problems in my code since I didn't fully consider all the possible inputs and the time was too limit for me to correct. 

 

Question Forth Interviewer Asked:

1.find all words in a string with no space based on a dictionary


I figured it out that this was a recursive problem and finally I wrote the code and also he provided some instructions.He was very kind and I communicated well with him.

This time I have more communications with interviewers and wrote the code more carefully.