Today is my first phone interview since I start to look for jobs. I feel so nerves before the clock run to 10:00 am. Actually, zzz and I go through the interview question till Midnight at two o‘clock.
My phone interviewer is a mixed race girl, I think that she must have Chinese descent. Her accent is perfect, I can understand her meaning clearly.
Firstly, she talked about her jobs at LinkedIn, what she does, what she teams do. I didn‘t catch up all the things she said.
Then, she let me said my background. Do not feel nervous, it is just a warm up.
Thirdly, she gives me a smoke test problem let me solve it. There are two problems: Two Sum, Shortest Word distance
Two Sum.
This is a classical problem in Leetcode. The problem same as the word "abandon" in CET-6. Each programmer starts to find a job, they all need to solve problems on LeetCode OJ.
But this one is an upgrade one. Here is a good blog write about this problem. Thanks for grandyang, his LeetCode blog is so helpful, he shares the LeetCode premium problems.
After I give her an O(n) / test(), O(1) / store() solution, she want me to implement another one. O(1) / test(), O(?) not matter / store. What I need to do is pre process the input when store() it.
http://www.cnblogs.com/grandyang/p/5184143.html
Some hints:
- When she let me go through my code, my thought is not so clear. And not working at all. so said the wrong test answer. Calm Down.
When she argues me that the first solution, line9, I should go through the code carefully because my code has no bug, the hint she gives will cause bugs.
if (second == p.first) return p.second >= 2;
1 class TwoSum { //O(n) / test() O(1) / store() 2 void store(int input) { // time complexity O(1) 3 numbers[input]++; 4 } 5 bool test(int input) { // time complexity O(n) 6 if (numbers.size() == 0) return false; 7 for(auto p : numbers){ 8 int second = input - p.first; 9 if (second == p.first) return p.second >= 2; 10 if (numbers.find(second) != numbers.end()) { 11 return true; 12 } 13 } 14 return false; 15 } 16 unordered_map<int, int> numbers; 17 };
1 class TwoSum { // O(1) / test() 2 void store(int input) { 3 for(auto n : numbers) { 4 sum.insert(input + n); 5 } 6 numbers.push_back(input); 7 } 8 bool test(int input) { 9 if (sum.empty()) return false; 10 return sum.find(input) != sum.end(); 11 } 12 unordered_set<int> sum; 13 vetor<int> numbers; 14 };
WordDistanceFinder
/* This class will be given a list of words (such as might be tokenized
* from a paragraph of text), and will provide a method that takes two
* words and returns the shortest distance (in words) between those two
* words in the provided text.
* Example:
* WordDistanceFinder finder = new WordDistanceFinder(Arrays.asList("the", "quick", "brown", "fox", "quick"));
* assert(finder.distance("fox", "the") == 3);
* assert(finder.distance("quick", "fox") == 1);
*
* "quick" appears twice in the input. There are two possible distance values for "quick" and "fox":
* (3 - 1) = 2 and (4 - 3) = 1.
* Since we have to return the shortest distance between the two words we return 1.
*/// wordOne == wordTwo -> 0
// word does not exist -> -1
Not bug-Free. The interviewer found a bug in my code.
1 class WordDistanceFinder { 2 public: 3 WorddistanceFinder(vector<string>& wordsList) { 4 for(int i = 0; i < wordsList.size(); i++) { 5 words[wordsList[i]].push_back(i); 6 } 7 } 8 int distance(string wordOne, string wordTwo) { 9 if (wordOne.size() == 0 || wordTwo.size() == 0) return -1; 10 //if (wordOne == wordTwo) return 0; bug 11 const vector<int> &v1 = words[wordOne]; 12 const vector<int> &v2 = words[wordTwo]; 13 if (v1.empty() || v2.empty()) return -1; 14 if (wordOne == wordTwo) return 0; // shoud check wordOne == wordTwo at here. 15 int p = 0, q = 0; 16 int ret = INT_MAX; 17 while(p < v1.size() && q < v2.size()) { 18 ret = min(ret, abs(v1[p] - v2[q])); 19 if (v1[p] < v2[q]) p++; 20 else q++; 21 } 22 return ret; 23 } 24 private: 25 unordered_map<string, vector<int>> words; 26 }
That‘s all my first technical interview. Hope I pass it and move on to onsite.