chapter 7 exercises
This commit is contained in:
parent
797fbae601
commit
af5d6abe26
|
|
@ -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.
|
||||
|
||||
|
|
|
|||
|
|
@ -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?
|
||||
Loading…
Reference in New Issue