Re: [問題] 公司考題 (觀念題??)

看板C_and_CPP作者 (天亮damody)時間12年前 (2013/04/04 04:25), 編輯推噓4(4017)
留言21則, 6人參與, 最新討論串2/2 (看更多)
※ 引述《LinRungChuan (吉他手)》之銘言: : 這是一家公司給我的題目 : 30 int * someIDs, theFirst, *r; : 110 someIDs =GetSomeIDs(); /* defined below */ : 111 theFirst = someIDs [0]; : 112 r= ReorderIDs(someIDs); : 113-150 /* we want to use 'theFirst' and 'r' here*/ : 499 /*-------- GetSomeIDs-----*/ : 500 int * GetSomeIDs() : 501 { : 502 int ids[8]; : 503-550 /* The ids are defined here */ : 551 return ids; : 552 } : Q1: Is there a different way to write line 500 which preserves : the same effective prototype? If so, what is it? : 我回答: Yes, we can replace int * by void GetSomeIDs(int *ids). : In that case, we don't need to re-declare int ids in line 502 : and no need line 551. : 結果公司給我個 hint 如下: : they are looking for an equivalent way to write line #500, : maintaining the same prototype : 可是我看完他的hint 我還是不懂他是要我用啥寫法取代 int * GetSomeIDs()...囧 : Q2: What will 'theFirst' contain after line 111 is executed? : Is this deterministic? Why? : 這個問題我回答 'theFirst' 是不是 deterministic 要看GetSomeIDs() : 裡面的寫法才能決定 : 結果公司說the answer is ‘deterministic’要我解釋why.... : 這邊我該如何解釋 'theFirst' 一定是 deterministic? : (其實我還是覺得不一定是 deterministic....=.=a...) : ------------------------------------------------------ : 這測試 是那公司的HR寄了 差不多15題的題目給我回答 大概給3天時間 : 可以上網或翻書 查任何需要的資料(但要付上來源) : 不過不能把題目直接po到網路~XD 他們查到就取消面試資格 : 我還真的有看到 有人把整分題目都po到網路問人...結果公司的人看到 : 在下面回他 他違反他簽的同意書 所以要把他的資格取消.... : 所以基本上我google過了 類似的問題,但還是沒辦法回答他們要的答案..>_<.. 個人練習官方說法: 因為區域變數的指標不能回傳 想要得到計算完的id 可以把型態改成 int[8] 用上 typedef typedef int int8s[8]; int8s GetSomeIDs() { int8s ids; /* The ids are defined here */ return ids8s; } 不知道是不是你要的答案。 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.230.139.178

04/04 07:50, , 1F
這也是一個方法 我會參考參考 感謝 但我還是不懂他
04/04 07:50, 1F

04/04 07:51, , 2F
指的line 500 要保持 the same prototype是啥意思...
04/04 07:51, 2F

04/04 07:52, , 3F
不過我問了一下他們HR 他回我 hint就只是hint 如果
04/04 07:52, 3F

04/04 07:53, , 4F
我覺得我的回答不用修改 可以不要管hint@@~感覺HR不
04/04 07:53, 4F

04/04 07:53, , 5F
懂那部門要的意思 亂敷衍我=.=
04/04 07:53, 5F

04/04 11:03, , 6F
HR本來就不會懂程式吧~~
04/04 11:03, 6F

04/04 11:13, , 7F
改成int&呢?
04/04 11:13, 7F

04/04 12:42, , 8F
函式不能回傳一個"陣列"吧?
04/04 12:42, 8F
int8s 不是陣列 是 結構

04/04 15:55, , 9F
他應該是漏打* 我想用typedef回答應該是對方要的
04/04 15:55, 9F

04/04 15:55, , 10F
因為前面題目有先考typedef,而且用這樣寫
04/04 15:55, 10F

04/04 15:56, , 11F
int *GetSomeIDs() 這行都不會改到
04/04 15:56, 11F

04/04 15:57, , 12F
應該算the same prototype了吧@@!!!
04/04 15:57, 12F

04/04 16:01, , 13F
不對~他應該不是漏掉*,應該是打錯...
04/04 16:01, 13F

04/04 16:01, , 14F
int8s GetSomeIDs()維持int *GetSomeIDs()就能傳了!!
04/04 16:01, 14F
沒有打錯 編譯的過(我錯了) ※ 編輯: damody 來自: 140.118.175.32 (04/04 19:05)

04/04 19:14, , 15F
我用typedef int int8s[8];
04/04 19:14, 15F

04/04 19:15, , 16F
int8s GetSomeIDs() 這樣去編譯 不會過耶@@!!!
04/04 19:15, 16F

04/04 19:16, , 17F
而且訊息像h大說的 寫了function returns array
04/04 19:16, 17F
真的耶 不能當函數回傳值

04/04 20:32, , 18F
題目本身就出得像外行人寫的,還不懂裝懂用英文
04/04 20:32, 18F

04/04 21:05, , 19F
公司在美國~他們一定得英文出題,題目很不順 到是真的
04/04 21:05, 19F

04/04 21:06, , 20F
出題的人 英文應該不是母語=.=...看的好累
04/04 21:06, 20F

04/04 21:09, , 21F
最好是 int8s 是結構...
04/04 21:09, 21F
真的不是耶,原來還是陣列 換個方法好了, 方法一. int* GetSomeIDs() { static int ids[8]; /* OOXX */ return ids; } 方法二. int* GetSomeIDs() { int* ids = (int*)malloc(sizeof(int) * 8); /* OOXX */ return ids; } 方法一有 多執行緒或是 不能同時呼叫兩次的問題 像是 int* id1 = GetSomeIDs(); int* id2 = GetSomeIDs(); 就會錯了 方法二要自己 free 掉 出這種題目可能要加入大量限制 讓解題者的解題方向如預期嗎? > < ※ 編輯: damody 來自: 140.118.175.32 (04/04 21:46)
文章代碼(AID): #1HN911Z5 (C_and_CPP)
文章代碼(AID): #1HN911Z5 (C_and_CPP)