I recently finished a tech interview with a local small company. Here is a brief summary for the questions in the interview.
- There are n couples invited in a party, husband must come with wives, wives may come alone, what is number of possible gathering?
solution:
we inspect a single family - there are 3 cases:
- no one comes
- only wife comes
- both of them comes
so the number of possible gathering is 3^n.
- Detect cycle in a linked list with constant space complexity
Solution:
use the fast/slow pointer idea.
1 | bool hasCycle(ListNode *head) { |
- Find within a binary array (sorted) the number of 0
Case:
0 0 0 0 0 1
0 1 2 3 4 5
1 1 1 1 1 1
0 1 2 3 4 5
Solution:
use binary search.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20int getNumberOfZero(vector<int> array){
int l = 0, r = array.size();
while(r - l > 1) {
int mid = (r+l)/2;
if (array[mid] == 0) {
l = mid;
}
else {
r = mid;
}
}
if (array[l] == 0) {
return l+1;
}
else {
return l;
}
}
- there is an array,only one element is not repeated;the other elements are repeated and repeated elements are next to each other; find not repeated element.
Case:
4 4 2 2 0 3 3 1 1
0 1 2 3 4 5 6 7 8
Solution:
inspect a property that
before single element: array[x] == array[x+1] if (x % 2 == 0)
after single element: array[x] != array[x+1] if (x % 2 == 0)
use binary search to secure the single element.
1 | int findNotRepeatedElement(vector<int> array) { |
- there is a square matrix, rotate it by 90 degree, do it in-place
Case:
1 2 3
4 5 6
7 8 9
=>
3 6 9
2 5 8
1 4 7
Solution:
first doing a mirror image by swapping every column, then swap by diagnoal line.
1 | void rotateBy90Degree(vector<vector<int>>& matrix) { |
6.
extension of N lion-goat-hay problem.
The size of the boat has to be at least $floor(\frac{N}{2})$ to solve this.
Assuming the items are indexed as $N_1, N_2, … N_n$ .
For the case which $n$ is an even number, it requires a constant move of 3 to solve this puzzle.
- move with all even-indexed items.
- go back with an empty boat.
- move with all odd-indexed items.
This is quite straight forward.
For the case which $n$ is an odd number, It requires a constant move of 7 to solve this puzzle.
The key concept is to take $N_1$ as the “lion”, even-indexed items as the “goat”, odd-indexed items except $N_1$ as the “hay”. In short, no matter how large $n$ is, the moves required is always 7. In other words, with a constant time complexity of $O(1)$.
- move with all even-indexed items(“goat”).
- go back with an empty boat.
- move with all odd-indexed item except $N_1$(“hay”).
- go back with all even-indexed items(“goat”).
- move with $N_1$(“lion”).
- go back with an empty boat.
- move with all even-indexed items(“goat”).
Step 3 and step 5 can be swapped. The total number of moves should not be changed by this. Moreover, odd-indexed items can be splited arbitrarily into two non-empty groups and be treated as “lion” and “hay”. The conclusion and steps remain the same.
- clarification of class/interface/abstract class in Java.
class: A bunch of properties and methods that are common to all the objects that are of the same type.
interface: A blueprint of class with ONLY abstract methods. It describes the general idea for a class to be made and cannot have concrete methods.
abstract class: A class with abstract keyword. It is different from normal class that it should have at least one abstract(not-yet-implemented) method. Or it can be considered as an interface with some concrete method.
In a way, the idea of interface can be described as “pure abstract class” or to say that abstract class is a halfly implemented class; but interface and abstract class are used for different purposes. Abstract class is mainly used for inheritance and it is relatively faster; interface are used for future enhancement and it is relatively slow. A class can implement multiple interface, but one class can only extend from a certain abstract class.