Thursday, July 30, 2015

Design data structures for an online book reader system ~ KodeKnight



Design data structures for an online book reader system ~ KodeKnight
Design the data structures for an online book reader system.

public class Book {
    private long ID;
    private String details;
     
  
    public long getID() {
        return ID;
    }
  
    public Book(long id, String details) {
    }
}
public class User {
    private long ID;
    private String details;
    private int accountType;
    
    public long getID() {
        return ID;
    }
  
    public Book searchLibrary(long id) {
        return Book.find(id);
    }
  
    public void renewMembership() {
    }
}

Managing books :
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class Books{
 private Set<Book> books;
  
 public void addBook(long iD, String details) {
        books.add(new Book(iD, details));
    }
  
    public void update() {
    }
  
    public void delete(Book b) {
        books.remove(b);
    }
  
    public Book find(long id) {
        for (Book b : books)
            if (b.getID() == id)
                return b;
        return null;
    }
}
managing users : 
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class Users {
    private Set<User> users;
  
    public Book searchLibrary(long id) {
        return Book.find(id);
    }
  
    public void renewMembership() {
    }
  
    public static User find(long ID) {
        for (User u : users) {
            if (u.getID() == ID)
                return u;
        }
        return null;
    }
  
    public void addUser(long ID, String details,
            int accountType) {
        users.add(new User(ID, details, accountType));
    }
  
    public User(long iD, String details, int accountType) {
    }
}

Now we need the 1:1 mapping between book and user, which is managed by Book User, and online reader finally manages everything : 
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
public class BookUsers {
  
 private HashMap<Book,Set<User>> bookUsers;
 private HashMap<User,Set<Book>> userBooks;
  
 private void addUserToBookUserList(Book b, User u){
  if(bookUsers.containsKey(b)){
   Set<User> users = bookUsers.getValue(b);
   users.add(u);
  }else
  {
   bookUsers.add(b,new HashMap<User> (){u});
  }
 }
  
 private void addBookToUserBookList(Book b, User u){
  if(userBooks.containsKey(u)){
   Set<Book> books = userBooks.getValue(u);
   books.add(b);
  }else
  {
   userBooks.add(u,new HashMap<Book> (){b});
  }
 }
 public void assignBookToUser(Book b, User u)
 {
  addUserToBookUserList(b,u);
  addBookToUserBookList(b,u);
    
 }
}
  
public class OnlineReaderSystem {
    private Books books;
    private Users users;
 private BookUsers bookUsers;
  
 private HashMap<Book,Set<User>> bookUsers;
 private HashMap<User,Set<Book>> userBooks;
  
 public void assignBookToUser()
 {
  bookUsers.assignBookToUser(b,u);
    
 }
  
    public OnlineReaderSystem(Books books, Users users) {
    }
  
    public void listenRequest() {
    }
  
    public Book searchBook(long ID) {
        return books.find(ID);
    }
  
    public User searchUser(long ID) {
        return users.find(ID);
    }
  
    public void display() {
    }
  
}

Unfortunately, BookUser will change when we take into account db relational model, as we dont have to keep things in-memory. 
https://xmruibi.gitbooks.io/interview-preparation-notes/content/OOD/DesignExamples/OnlineReader.html

This question comes from the book named "Cracking Code Interview", Chapter 7; It is very easy problem with thinking about the insert/remove/update/retrieve action.
    Functionality
  • User Membership Creation and Extension
  • Search the book in memory
  • Reading the book
Objects
  • Book:
    • ID
    • Title
    • Author
    • Content
    • Method
      • showContent
  • Books: (In-memory storage for many book objects)
    • Set
    • Method
      • find
      • add
      • delete
      • update
  • User
    • ID
    • Name
    • accoutnType
    • Method
      • findBook
      • read (book.showContent)
  • Users
    • Set
    • Method
      • find
      • add
      • delete
      • update
Read full article from Design data structures for an online book reader system ~ KodeKnight

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