Thursday, November 12, 2015

Design Telephone System



https://gist.github.com/gcrfelix/b84e173c83d4f79068a4
设计一个电话本系统,实现三个功能:
查找号码 boolean isTaken(),
添加号码 void takeNumber(),
返回一个不在电话本里的没人用的号码 number giveMeANumber()。
==> random, reservoir sampling
用的是Trie and boolean array, 不用Hashmap是因为 map的initial size是256,对于这种只需要存0-9十个数的情况就很不划算了
用trie只需要存每层0-9十个boolean,如果这个数已用就把相应的index set一下
用boolean array更省地方,arrays of booleans use 1 byte per element
Search Time Complexity: O(length of phone number)
Insert Time Complexity: O(length of phone number)

public class Numbers{
        Node root;
        public Numbers(){
              root=new Node();
        }
}
class Node{
      boolean[] children;
      public Node(){
            children=new boolean[10];
       }
}
http://sudhansu-codezone.blogspot.com/2012/09/phone-book-data-structure.html
Using a Trie can help you on that and the time will be O(L) where L is the string length.
1) input name and find phone number
-- Trie structure for name (string), phone number as assocaited value.
-- Or hashtable (name as hash key), and link list as chaining (for name duplicatiion)
2) input phone number and find name
-- hash table
(phone number is unique, so use as hash key)

The great thing about a trie is that it would provide functionality to easily facilitate partial searches, you type in the start of the persons name or phone number and you could return all leaf nodes that match that that prefix. Insertion and search will take O(length of phone number). To display the numbers starting with one or more digits, then the complexity will be the O(prefix length)+O(number of children for the immediate prefix node * length of the remaining number from child nodes).

https://instant.1point3acres.com/thread/131215/posts?ps=
Use too much space.
维护一个HashSet taken,其中是已被占用的号码;另外维护一个LinkedHashMap untaken。其中是未被占用的号码,它由两部分组成:

Doubly Linked List: (num1) <-> (num2) <-> (num3) <-> … <-> (numN)
HashMap: {num1: &(num1), num2: &(num2), …}即将数字映射成到链表节点的指针。

isTaken(): 可以简单地通过查询taken得到。O(1)

takeNumber(n): 可以从HashMap中找到n,和其对应的链表节点Node,从链表和HashMap中分别删除Node和n,以上都是O(1)操作。然后将n加入taken,这步也是O(1)。总时间O(1)。另一方面,如果n不在untaken中,也可以用O(1)时间获知。

giveMeANumber(); pop出List的第一个即可。同样是O(1)
https://codingarms.wordpress.com/2015/05/01/design-a-data-structure-to-implement-a-phone-book-can-search-by-name-or-number/

Labels

Review (572) System Design (334) System Design - Review (198) Java (189) Coding (75) Interview-System Design (65) Interview (63) Book Notes (59) Coding - Review (59) to-do (45) Linux (43) Knowledge (39) Interview-Java (35) Knowledge - Review (32) Database (31) Design Patterns (31) Big Data (29) Product Architecture (28) MultiThread (27) Soft Skills (27) Concurrency (26) Cracking Code Interview (26) Miscs (25) Distributed (24) OOD Design (24) Google (23) Career (22) Interview - Review (21) Java - Code (21) Operating System (21) Interview Q&A (20) System Design - Practice (20) Tips (19) Algorithm (17) Company - Facebook (17) Security (17) How to Ace Interview (16) Brain Teaser (14) Linux - Shell (14) Redis (14) Testing (14) Tools (14) Code Quality (13) Search (13) Spark (13) Spring (13) Company - LinkedIn (12) How to (12) Interview-Database (12) Interview-Operating System (12) Solr (12) Architecture Principles (11) Resource (10) Amazon (9) Cache (9) Git (9) Interview - MultiThread (9) Scalability (9) Trouble Shooting (9) Web Dev (9) Architecture Model (8) Better Programmer (8) Cassandra (8) Company - Uber (8) Java67 (8) Math (8) OO Design principles (8) SOLID (8) Design (7) Interview Corner (7) JVM (7) Java Basics (7) Kafka (7) Mac (7) Machine Learning (7) NoSQL (7) C++ (6) Chrome (6) File System (6) Highscalability (6) How to Better (6) Network (6) Restful (6) CareerCup (5) Code Review (5) Hash (5) How to Interview (5) JDK Source Code (5) JavaScript (5) Leetcode (5) Must Known (5) Python (5)

Popular Posts