ThoughtWorks筆試題大致解題思路總結

收到ThoughtWorks的面試邀請,HR電話初面後,說是要做題。

給發了3道題,任選一道。

ThoughtWorks是什麼樣的公司呢?外企,聽說很牛,什麼“敏捷開發模式”就是那公司

首創的概念。出的題目也有些奇怪,選取第一道如下:

Problem one: Trains

The local commuter railroad services a number of towns in Kiwiland. Because of monetary concerns, all of the tracks are ‘one-way.’ That is, a route from Kaitaia to Invercargill does not imply the existence of a route from Invercargill to Kaitaia. In fact, even if both of these routes do happen to exist, they are distinct and are not necessarily the same distance!

The purpose of this problem is to help the railroad provide its customers with information about the routes. In particular, you will compute the distance along a certain route, the number of different routes between two towns, and the shortest route between two towns.

Input: A directed graph where a node represents a town and an edge represents a route between two towns. The weighting of the edge represents the distance between the two towns. A given route will never appear more than once, and for a given route, the starting and ending town will not be the same town.

Output: For test input 1 through 5, if no such route exists, output ‘NO SUCH ROUTE’. Otherwise, follow the route as given; do not make any extra stops! For example, the first problem means to start at city A, then travel directly to city B (a distance of 5), then directly to city C (a distance of 4).

  1. The distance of the route A-B-C.
  2. The distance of the route A-D.
  3. The distance of the route A-D-C.
  4. The distance of the route A-E-B-C-D.
  5. The distance of the route A-E-D.
  6. The number of trips starting at C and ending at C with a maximum of 3 stops. In the sample data below, there are two such trips: C-D-C (2 stops). and C-E-B-C (3 stops).
  7. The number of trips starting at A and ending at C with exactly 4 stops. In the sample data below, there are three such trips: A to C (via B,C,D); A to C (via D,C,D); and A to C (via D,E,B).
  8. The length of the shortest route (in terms of distance to travel) from A to C.
  9. The length of the shortest route (in terms of distance to travel) from B to B.
  10. The number of different routes from C to C with a distance of less than 30. In the sample data, the trips are: CDC, CEBC, CEBCDC, CDCEBC, CDEBC, CEBCEBC, CEBCEBCEBC.

Test Input:

For the test input, the towns are named using the first few letters of the alphabet from A to D. A route between two towns (A to B) with a distance of 5 is represented as AB5.

Graph: AB5, BC4, CD8, DC8, DE6, AD5, CE2, EB3, AE7

Expected Output:

Output #1: 9

Output #2: 5

Output #3: 13

Output #4: 22

Output #5: NO SUCH ROUTE

Output #6: 2

Output #7: 3

Output #8: 9

Output #9: 9

Output #10: 7

看題目,一開始摸不着頭腦,甚至用了翻譯琢磨。做完第一道提交了,具體對不對,

估計不好說。現把自己總結的解題思路寫下來,留個念。

問題大致分5類:

1.求路徑距離;
2.給定起始點,求路徑條數;
3.給定遍歷深度,求路徑條數;
4.給定路徑長度,求路徑條數;
5.求最短路徑

這道題考察的基礎有很多:

1.項目結構

  • 包的分類,命名
  • 業務分層
  • 依賴管理

2.數據結構設計

  • 數據定義
  • 選擇哪幾種集合
  • 集合的使用方法

3.程序設計

  • 遍歷,查找
  • 複雜度

4.異常處理

  • 自定義異常
  • 拋出異常

5.單元測試

1.項目結構

項目結構,分了entity,service,test,exception等幾個包,分別存放不同功能的class。依賴,最好使用maven做依賴管理。

2.數據結構設計

定義了town(城鎮),route(路線)等基本對象。集合選擇,(k,v)結構的,使用HashMap;單純的無序集合,使用HashSet;對集合的查找,遍歷,也用到了ArrayList。

3.程序設計

大致知道程序需要用到深度遍歷,廣度遍歷等思路。我看了二叉樹的遍歷,說是,廣度遍歷,是使用隊列;深度遍歷,是
使用堆棧。但這裏,其實是樹遍歷,其實還用到迭代遍歷。迭代遍歷不好理解。總之,也借鑑了別人的設計。

我也想到了複雜度這一點。O(N)當然是最好。但既有樹遍歷,又有迭代遍歷,複雜度是O(N)的N次方。感覺這點無解,之前做的少,不知道有什麼優化版本。查找的話,個人傾向for循環遍歷。

4.異常處理

應該是要自定一個異常,然後打印輸出異常情況。後來想,應該把打印輸出,也寫在自定異常類裏面。可是提交答案的時候,忘記做了。

異常使用throw關鍵字。沒有用try catch

5.單元測試

這裏面,一開始我以爲很簡單,但看了下別人的處理,感覺這裏的細節也很多。諸如類的初始化,註解的使用,測試方法的排序。

總之,這道題考察的基礎有很多,除了上面提到的,應該還有一些,限本人能力,一時還看不出。就先記下吧。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章