前言
上周参加了学校的 Career Fair,想锻炼下面试技巧和口语能力,为下学期找暑期实习做点准备。那天整条路都是熙熙攘攘的面试官和学生,Facebook、Amazon、Microsoft、eBay 这些大公司尤其火爆,有的人排了几个小时才面试上。
因为我刚来 USC 两个月,实力不够,而且大公司又有冷冻期(一次没有录上半年内不再面试),所以我也没敢投大公司,挑了几个看起来不错的小公司试试水。一共投了五个,拿到 Percolate 和 Paypal 两家的 Coding Challenge,还有一家 Demand Media 说是非常喜欢我,之后会打电话让我去 onsite interview(为啥现在还没打电话!)。
这道题是 Percolate 的题,名字叫 Funky Rolodex(复杂的关系网) 并没有考算法,而是非常贴近实际需求,处理文本文件,筛选出正确的数据解析并输出成JSON格式。虽然并不难,但是需要注意的小细节不少,譬如输出结果要按照相应地缩进,要对数据按照 Lastname 和 Firstname 排序。我之前没有注意到排序这一点,导致后来重构了一些代码,而且对我来说,排序这里是最难的(后面会解释)。
总体来说这是一道非常好的非算法题,考到了JSON,正则表达式,面向对象编程,字符串比较等等,而且这道题非常考察细心程度。
Problem Description
The Problem
You’re tasked with taking entries of personal information in multiple formats
and normalizing each entry into a standard JSON format. Write your formatted,
valid JSON out to a file with two-space indentation and keys sorted
alphabetically.
Input
Your program will be fed an input file of n lines. Each line contains “entry”
information, which consists of a first name, last name, phone number, color,
and zip code. The order and format of these lines vary in three separate
ways.
The three different formats are as follows:
- Lastname, Firstname, (703)-742-0996, Blue, 10013
- Firstname Lastname, Red, 11237, 703 955 0373
- Firstname, Lastname, 10013, 646 111 0101, Green
Some lines may be invalid and should not interfere with the processing of
subsequent valid lines. A line should be considered invalid if its phone
number does not contain the proper number of digits.
Output
The program should write a valid, formatted JSON object. The JSON
representation should be indented with two spaces and the keys should be sorted
in ascending order.
Successfully processed lines should result in a normalized addition to the list
associated with the “entries” key. For lines that were unable to be processed,
a line number i (where 0 ≤ i < n) for each faulty line should be appended to
the list associated with the “errors” key.
The “entries” list should be sorted in ascending alphabetical order by (last
name, first name).
The complete output schema is specified below.
Sample
For the input
1 | Booker T., Washington, 87360, 373 781 7380, yellow |
we should receive the output1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22{
"entries": [
{
"color": "yellow",
"firstname": "James",
"lastname": "Murphy",
"phonenumber": "018-154-6474",
"zipcode": "83880"
},
{
"color": "yellow",
"firstname": "Booker T.",
"lastname": "Washington",
"phonenumber": "373-781-7380",
"zipcode": "87360"
}
],
"errors": [
1,
3
]
}
Solution
1 | process.stdin.resume(); |
Testcases
Download Link
Usage
1 | /** |