1. Java Code Review and Improvement
You are given a Java code snippet and asked to identify its purpose and suggest improvements. The code is as follows:
public class Content{
}
public Content parseLine(String[] input) {
//fake function
}
public Map map = new HashMap();
public Content parse(String[] input) {
Content content = map.get(input);
if(content == null) {
synchronized (map) {
content = map.get(input);
if(content == null) {
content = parseLine(input);
map.put(input, content);
}
}
}
return map.get(content);
}
Additionally, you may be asked about detailed aspects of Java, such as the hashcode function and the differences in memory storage between arraylist and linkedlist.
2. Implement PubSub Classes
Implement PubSub subscription class and subject class. A subscription is a lambda function which takes one input. A subject can have multiple subscriptions. Implement the next function of a subject that calls all its subscriptions with the provided parameter, and an unsubscribe function to remove a subscription.
3. Implement a Rate Limiter
Implement a rate limiter class. The input is the limit of the number of requests per second. When a caller calls the available function, judge if the request breaches the limit. If so, return false; otherwise return true.
4. Design a Counter System
Design a Counter System with a focus on reliability, especially considering network failures, and then discuss how to scale the system.
5. Distributed Counter App Design
Design a web app that allows you to increment a counter collaboratively. The app serves many counters, and multiple users can collaborate on a single counter. Each counter should have a unique ID with a corresponding URL. On a counter page, anyone can click the [ + ] button and see the result immediately, while other participants would see the change soon after. Start with a solution to process counters reliably, considering network failures. Describe the API, Database Schema, and Server-side logic in pseudo-code. Ensure correctness and final consistency is the top priority, even if there is a delay on processing and network issues. After establishing a resilient implementation, consider scalability for 10M concurrent users, with about 100 users per counter, and roughly 1 click per user per second. Discuss how to achieve low latency to see clicks as soon as possible.