chapter 7 exercises

This commit is contained in:
anebz 2020-05-21 20:48:47 +02:00
parent 797fbae601
commit af5d6abe26
4 changed files with 53 additions and 1 deletions

View File

@ -15,4 +15,3 @@ I don't understand the worst case scenario.
> It's actually better for the first drop to be a bit lower. For example, you could drop at the 10th floor, then the 20th floor, then the 30th floor, and so on.The worst case here will be 19 drops (10, 20, ..., 100, 91, 92, ... , 99). Can you beat that? Try not randomly guessing at different solutions. Rather, think deeper. How is the worst case defined? How does the number of drops of each egg factor into that?
Still don't get it.

View File

@ -0,0 +1,50 @@
# 7.7. Chat server
> Explain how you would design a chat server. In particular, provide details about the various backend components, classes, and methods. What would be the hardest problems to solve?
Hints 213, 245, 271
```java
public class chatServer {
private ArrayList<User> allUsers;
void startChat();
void createUser(String username){
allUsers.push(new User(username));
}
}
public class User {
private int id;
private String username;
private ArraList<User> allContacts;
private ArrayList<Conversation> allConversations; // should be a hashmap
public User(String username) {
this.id = rand();
this.username = username;
this.allContacts = new ArrayList();
this.allConversations = new ArrayList();
}
void createConversation(String username);
void sendMessage(String username) {
this.allConversations[username].sendMessage();
}
}
public class Conversation {
private ArrayList<User> participants;
private ArrayList<Object> sharedFiles;
}
```
## Solution
The key objects of the system are users, conversations and status messages.
* How to know if someone is online?
* Regularly ping the client
* How do we deal with conflicting information? There's info stored in the computer's memory and some other in the database, what if they get out of sync, which one is right?
* How do we make the server scale?
* Split the data across many servers, incresing the concern about out-of-sync data
* How do we prevent DoS attacks?

View File

@ -63,6 +63,9 @@ If you can't afford to buy the book, you can find a free pdf [here](http://ahmed
## Chapter 7 Object-oriented design
* Call center
* Parking lot
* Chat server
* Hash tables
## Chapter 8 Recursion