您的位置:首页 > 其它

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: