Saturday, July 22, 2006

2nd Phone Screen with Amazon.com

The interviewer started asking technical questions right away without wasting any time.

1. When whould you create an interface class.
I got this wrong and thought any chance of getting this job is already over. My answer was when I wanted to have a single interface to many derived classes. This was wrong.
He said how would you define many? I said that it was really a design decision which also depended on how many derived classes you anticipated to have in the future. He told me that it had nothing to do with the number of derived classes but rather any class that has more than one derived class should have an interface. He then gave an example of having a unix and a windows class which would both inheret from the OS class.

2. How would a copy constructor behave if the class had an Object as a member Variable?
I nailed this one by saying that you would not use the default copy constructor in this case since the new object would have its object member variable point to the previously instantiated object, which is not what we want. We would have to make our own copy constructor and make a new Object pointer which would be dereferenced to point to the desired value.
Object * Optr = new Object;
*Optr = *rhs.Optr;

3. Write a function that would find the one value that occurs an odd number of times in an array. ie; function would return "r" if "ttttjjrll" is passed in.
I had read about a similar problem in PIE which would find the value that only occured once in the array. I first hesitated, then I stumbled into actually writing a function for it, when he told me to just tell him in words how I would implement it.
Since I knew that time complexity is the most important part of this question, I told him that I would create a hash table, then I would iterate through the array. Everytime I hit a value I would increment a count variable in the hashtable for that value. Then I would use modulus 2 to find out which count variable is an odd number.
He was impressed by this answer, and asked me about the time and space complexities, and I told him that they were both O(n). I explained for the time complexity, I had to iterate through the array once O(n) and that the lookup to the hash table was in constant time O(1). The space complexity was also minimized a hashtable would only create a count variable for values that actually existed in the array. He then told me that I could reduce the number of executions while keeping the time and space complexities linear. I got stuck here, and he moved on.
I think now that by making the count value into a bool and toggling its value each time, I would not need the modulus operation and would decrease the number of executions.

4. How would you design a file system using class diagrams and what data structure would you use?
When he asked me this, I got a little nervous. I then came to my senses and said that the only classes I could think of are a file class and a directory class. He seemed to be satisfied with that. I then said I would have a 'has a' relationship since a directory contains 0..* files. He wasnt impressed. He told me to simplify the design. I told him that I couldn't think of anything else since it doesn't make sense to have file inheret from directory or have directory inheret form file. He then told me that in Unix a directory is just another file. I said, Ok, then I would have directory inheret from file with the file class having a vector of files as a member variable. He didn't continue on with this, and asked me about the data structure I would use. I told him that from my readings, I knew that file systems use a tree and that made sense because every directory (which is a kind of file) has a parent directory and children. The time for the interview was over at this time and he seemed to be generally pleased with my answers.
I then asked him a few questions about how things are at Amazon and the interview was over. I told him that this was my second screening and asked if there were going to be more screening. He said that my next interview would probably be in Seattle. Done and done.