CareerCup.Cracking.the.Technical.Interview summaries
2011-03-09 14:04
495 查看
CareerCup.Cracking.the.Technical.Interview summaries
//////////////////////////////////////
// string
//////////////////////////////////////
1) check duplicated character
>> use ASCII table
//////////////////////////////////////
// single linked list
//////////////////////////////////////
2) remove duplicate string in a linked list
>> use two pointers, "current" and "previous_dup"
3) find nth to the last of single linked list
>> use p1 = head, p2 = head + n-1, increment p1 and p2, if p2 read the end, then p1 point the nth to the last
4) delete the node in the middle of single linked list, given only access of that node?
>> copy next node data to this node then delete next node.
5)
Given a circular linked list, implement an algorithm which returns node at the beginning of the loop.
DEFINITION
Circular linked list: A (corrupt) linked list in which a node's next pointer points to an earlier node, so as to make a loop in the linked list.
EXAMPLE
Input: A -> B -> C -> D -> E -> C [the same C as earlier]
Output: C
>>
If we move two pointers, one with speed 1 and another with speed 2, they will end up meeting if the linked list has a loop.
//////////////////////////////////////
// stack and queue
//////////////////////////////////////
6) How would you design a stack which, in addition to push and pop, also has a function
min which returns the minimum element? Push, pop and min should all operate in
O(1) time.
>>
design the node as
NodeWithMin
{
T value;
T min;
};
when pushing into the stack, also update the min of the node
7) implement a queue using 2 stacks
>>
keep oldest element in A
keep newest element in B
queue::enqueue(): push data to A
queue::dequeue(): pop data from B, if B is empty, move data from A to B in reverse order
8) sort a stack
>> using another stack for helping
stack<int> s1;
s1.push(3);
s1.push(6);
s1.push(2);
stack<int> smallest;
while(!s1.empty())
{
int val = s1.top();
s1.pop();
if(!smallest.empty() && smallest.top() > val)
{
s1.push(smallest.top());
smallest.pop();
}
smallest.push(val);
}
while(!smallest.empty())
{
cout << smallest.top() << endl;
smallest.pop();
}
//////////////////////////////////////
// tree and graph
//////////////////////////////////////
9) Breath first search/ Depth first search
10) build heap
//////////////////////////////////////
// brain teasers
//////////////////////////////////////
6.5 There is a building of 100 floors. If an egg drops from the Nth floor or above it will
break. If its dropped from any floor below, it will not break. You are given 2 eggs. Find
N, while minimizing the number of drops for the worst case.
//////////////////////////////////////
// sorting and searching
//////////////////////////////////////
You are given two sorted arrays, A and B, and A has a large enough bu$er at the end
to hold B. Write a method to merge B into A in sorted order.
>> merge sort, start from the back
//////////////////////////////////////
// string
//////////////////////////////////////
1) check duplicated character
>> use ASCII table
//////////////////////////////////////
// single linked list
//////////////////////////////////////
2) remove duplicate string in a linked list
>> use two pointers, "current" and "previous_dup"
3) find nth to the last of single linked list
>> use p1 = head, p2 = head + n-1, increment p1 and p2, if p2 read the end, then p1 point the nth to the last
4) delete the node in the middle of single linked list, given only access of that node?
>> copy next node data to this node then delete next node.
5)
Given a circular linked list, implement an algorithm which returns node at the beginning of the loop.
DEFINITION
Circular linked list: A (corrupt) linked list in which a node's next pointer points to an earlier node, so as to make a loop in the linked list.
EXAMPLE
Input: A -> B -> C -> D -> E -> C [the same C as earlier]
Output: C
>>
If we move two pointers, one with speed 1 and another with speed 2, they will end up meeting if the linked list has a loop.
//////////////////////////////////////
// stack and queue
//////////////////////////////////////
6) How would you design a stack which, in addition to push and pop, also has a function
min which returns the minimum element? Push, pop and min should all operate in
O(1) time.
>>
design the node as
NodeWithMin
{
T value;
T min;
};
when pushing into the stack, also update the min of the node
7) implement a queue using 2 stacks
>>
keep oldest element in A
keep newest element in B
queue::enqueue(): push data to A
queue::dequeue(): pop data from B, if B is empty, move data from A to B in reverse order
8) sort a stack
>> using another stack for helping
stack<int> s1;
s1.push(3);
s1.push(6);
s1.push(2);
stack<int> smallest;
while(!s1.empty())
{
int val = s1.top();
s1.pop();
if(!smallest.empty() && smallest.top() > val)
{
s1.push(smallest.top());
smallest.pop();
}
smallest.push(val);
}
while(!smallest.empty())
{
cout << smallest.top() << endl;
smallest.pop();
}
//////////////////////////////////////
// tree and graph
//////////////////////////////////////
9) Breath first search/ Depth first search
10) build heap
//////////////////////////////////////
// brain teasers
//////////////////////////////////////
6.5 There is a building of 100 floors. If an egg drops from the Nth floor or above it will
break. If its dropped from any floor below, it will not break. You are given 2 eggs. Find
N, while minimizing the number of drops for the worst case.
//////////////////////////////////////
// sorting and searching
//////////////////////////////////////
You are given two sorted arrays, A and B, and A has a large enough bu$er at the end
to hold B. Write a method to merge B into A in sorted order.
>> merge sort, start from the back
相关文章推荐
- 《Cracking the Coding Interview》——第8章:面向对象设计——题目8
- Cracking the coding interview--Q20.7
- 《Cracking the Coding Interview》——第10章:可扩展性和存储空间限制——题目4
- Cracking the coding interview--Q20.10
- 《Cracking the Coding Interview》——第16章:线程与锁——题目4
- 《Cracking the Coding Interview》——第17章:普通题——题目2
- 《Cracking the Coding Interview》——第17章:普通题——题目12
- 《Cracking the Coding Interview》——第18章:难题——题目13
- CareerCup Find the biggest interval that has all its members in list in O(n)
- Cracking the Coding Interview 8.5
- Cracking the coding interview--Q1.3
- Cracking the coding interview--Q1.4
- Cracking the coding interview--Q8.7
- Cracking the coding interview--Q2.5
- Cracking the coding interview--Q2.3
- cracking the coding interview problem solution 1.5
- Cracking the coding interview 150 要点记录(一)--Array and List
- Cracking the coding interview--Q8.2
- Cracking the Coding Interview Q2.1
- cracking the code interview——c++实现