A data structure for a phone book such that it can search a number by name and also search a name by number

后端 未结 5 601
Happy的楠姐
Happy的楠姐 2021-01-12 07:52

Do you know a solution to the following interview question?

Design a data structure for a phone book that can safely and efficiently search a number

5条回答
  •  广开言路
    2021-01-12 08:25

    Isn't this easy?

    Using an array of record/struct/tuple of the value pair (telephone-number, name).

    1. Do a linear search looking for the search key; O(n/2) for match, O(n) for miss,

    2. return record/struct/tuple and do whatever needs to be done.

    Edit:
    This algorithm can be improved in many ways.

    I think this interview question might be deliberately under specified in order to find out how the interviewee reacts. (That is what I do when I interview). So it may be more important to treat it as such, rather than assume it is just a Computer Science question.

    I think it is worth engaging with the interviewer. For example:

    1. Is it important that this is fast enough for a personal telephone directory, e.g. on a mobile phone (unlikely any reasonable algorithm will be too slow), or a country-size telecom infrastructure application where there may be availability, concurrent update, and extra requirements, for example access control to some subset of names and numbers?
    2. What is the target technology? You might want to choose different approaches e.g. for Python, C++, assembler, ...
    3. Are their more qualities it needs to demonstrate than time & space efficiency? For example, must it be easy to prove correct, or test? Is the CPU-saving by a fast approach worth the human expense of testing time?
    4. How skilled or knowledgeable are the people maintaining and reusing code likely to be? Would simple approaches be favoured on maintenance cost grounds.

    etc.

    I think it is more important to engage with the interviewer, rather than only focusing on the technical solution. When I interview, I am looking for someone who tries to actually understand the whole problem rather than the (usually small) part which is easy to define.

提交回复
热议问题