CareerCup.Cracking.the.Technical.Interview summaries

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值