杭州睿琪軟件面試題

最近參加了一個面試,首先介紹一下這個公司是做軟件開發的,我個人對算法不是很擅長,結果被虐了哭。下面總結一下這次的面試題目,希望以後遇到同樣的問題可以參考。
問題1:我們都知道java是面向對象的,請問java當中所有類的父類及其擁有的方法。

這個問題想必大家都會,但卻很少有人能全部說出父類的方法。下面給出答案:

答:.所有類的父類是object類.在object子類中可以重寫其方法。具體方法列舉如下:

1:clone()方法,實現創建1個相同類型的對象。

2:equals()/hashcode():注意這2個方法必須同時重寫。

3:.finalize()。回收垃圾調用該方法。

4:toString()。返回對象的字符串,一般都需要重寫。

5:getClass()。返回對象的類信息。

6:wait().

7:notify().

其他的詳細詳細用法及介紹參考:http://www.blogjava.net/jiafang83/archive/2008/12/05/244510.html
問題2:單向鏈表怎麼判定是否有環?

這道題目我學數據結構根本沒練過,上課的時候老師也只是提過,沒有很詳細的講解,面試問到的時候頭都蒙了。哎。。。

給出答案:

方法一:使用p、q兩個指針,p總是向前走,但q每次都從頭開始走,對於每個節點,看p走的步數是否和q一樣。如圖,當p從6走到3時,用了6步,此時若q從head出發,則只需兩步就到3,因而步數不等,出現矛盾,存在環。

方法二:使用p、q兩個指針,p每次向前走一步,q每次向前走兩步,若在某個時候p == q,則存在環。

#include <stdio.h>
#include <stdlib.h>

#define LEN 8
typedef struct node* node_t;

struct node{  
    char val;  
    struct node *next;  
};  

//method 1
int has_loop(struct node *head);
//method 2
int has_loop2(node_t head);

int main()
{
    node_t* arr = (node_t*)malloc(sizeof(struct node)*LEN);
    arr[0] = (node_t)malloc(sizeof(struct node));
    int i;
    for(i = 1; i < LEN; i++)
    {
        arr[i] = (node_t)malloc(sizeof(struct node));
        arr[i - 1]->next = arr[i];
    }
    arr[LEN - 1]->next = NULL;

    //you can add a loop here to test
    //arr[6]->next = arr[0];
    if (has_loop(arr[0]))
        printf("method1: has loop.\n");
    else
        printf("method1: has no loop.\n");

    if (has_loop2(arr[0]))
        printf("method2: has loop.\n");
    else
        printf("method2: has no loop.\n");

    return 0;
}

//if two pointer are equal, but they don't have the same steps, then has a loop
int has_loop(node_t head)  
{  
    node_t cur1 = head;  
    int pos1 = 0;  
    while(cur1){  
        node_t cur2 = head;  
        int pos2 = 0;  
        pos1 ++;  
        while(cur2){  
            pos2 ++;  
            if(cur2 == cur1){  
                if(pos1 == pos2)  
                    break;  
                else  
                    return 1;
            }  
            cur2 = cur2->next;  
        }  
        cur1 = cur1->next;  
    }  
    return 0;  
} 

//using step1 and step2 here 
//if exists a loop, then the pointer which use step2 will catch up with the pointer which uses step1
int has_loop2(node_t head)
{
    node_t p = head;
    node_t q = head;
    while (p != NULL && q != NULL)
    {
        /*
        p = p->next;
        if (q->next != NULL)
            q = q->next->next;
        if (p == q)
            return 1;
            */
        //correct it on 17/11/2012
        p = p->next;
        q = q->next;
        if (q != NULL)
            q = q->next;
        if (p != NULL && p == q)
            return 1;
    }
    return 0;
}

3:java單例模式的一種寫法。
這個平時練習的很少,有些時候根本沒聽過模式設計,因爲平時具體代碼碰到的比較少,算是學習了。
這個大家百度一下就知道了,網上很多,設計模式有常見的23種,單例模式需要自己用代碼實現餓漢式和懶漢式的實現(面試的時候我就要求現場寫出代碼)。我寫不來。。

4.C++中指針和引用的區別

詳細的見這篇博客:別人分析的,寫的比較詳細http://blog.csdn.net/listening_music/article/details/6921608

發佈了21 篇原創文章 · 獲贊 2 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章