1st Technical : Implement Rate Limit
2nd Technical : Vote algorightm - Given votes of candidates sort them by most votes received
First vote have highest weight and subsequent votes have weight-1 then previous vote
Input : [[A, B, C
A, C, D
D,A,C]]
Output : [A, D , C , B]
This my solution
Solution1 :
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
// X request per Y seconds for each user
public class RateLimiterTest {
public static Map<Integer, Optional> map = new ConcurrentHashMap<>();
private int limit;
private int time;
private TimeUnit timeUnit;
public RateLimiterTest(int limit, int time, TimeUnit timeUnit) {
this.limit = limit;
this.time = time;
this.timeUnit = timeUnit;
}
public static void main(String args[]) {
RateLimiterTest test1 = createTest(5, 1, TimeUnit.SECONDS);
System.out.println(test1.rateLimit(1));
System.out.println(test1.rateLimit(1));
System.out.println(test1.rateLimit(1));
System.out.println(test1.rateLimit(1));
System.out.println(test1.rateLimit(2));
System.out.println(test1.rateLimit(3));
System.out.println(test1.rateLimit(2));
System.out.println(test1.rateLimit(1));
System.out.println(test1.rateLimit(1));
}
public static RateLimiterTest createTest(int limit, int time, TimeUnit timeUnit){
return new RateLimiterTest(limit, time, timeUnit);
}
public boolean rateLimit(int customerId){
if(customerId<=0){
return true;
}
boolean isRequestAllowed = this.getCustomerRateLimiter(customerId).tryAccess();
return isRequestAllowed;}
public CustomerRateLimiter getCustomerRateLimiter(int customerId){
if(map.containsKey(customerId)){
return map.get(customerId).get();
}
CustomerRateLimiter customerRateLimiter = CustomerRateLimiter.createCustomerRateLimiter(this.limit, this.time, this.timeUnit);
map.put(customerId, Optional.of(customerRateLimiter));
return customerRateLimiter;}
}
class CustomerRateLimiter {
private Semaphore semaphore;
private int maxLimit;
private int time;
private TimeUnit timeUnit;
private ScheduledExecutorService scheduledExecutorService;
public static CustomerRateLimiter createCustomerRateLimiter(int limit, int time, TimeUnit timeUnit){
CustomerRateLimiter customerRateLimiter = new CustomerRateLimiter(limit, time, timeUnit);
customerRateLimiter.scheduleRelease();
return customerRateLimiter;
}
private CustomerRateLimiter(int limit, int time, TimeUnit timeUnit){
this.semaphore = new Semaphore(limit);
this.maxLimit = limit;
this.time = time;
this.timeUnit = timeUnit;
}
public boolean tryAccess(){
return this.semaphore.tryAcquire();
}
//10, 2
private void scheduleRelease(){
this.scheduledExecutorService = Executors.newScheduledThreadPool(1);
scheduledExecutorService.schedule(() -> {
semaphore.release(this.maxLimit - semaphore.availablePermits());
}, time, timeUnit);
}
}
Solution 2 :
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;
/*
A, B, C
A, C, D
D,A,C
A, D , C , B
*/
public class HackathonTest {
public static void main(String args[]) {
List<List> candidates = new ArrayList<>();
List votes1 = new ArrayList<>();
votes1.add("A");
votes1.add("B");
votes1.add("C");
List<String> votes2 = new ArrayList<>();
votes2.add("A");
votes2.add("C");
votes2.add("D");
List<String> votes3 = new ArrayList<>();
votes3.add("D");
votes3.add("A");
votes3.add("C");
candidates.add(votes1);
candidates.add(votes2);
candidates.add(votes3);
System.out.println(sortedWinner(candidates));
List<List<String>> candidates1 = new ArrayList<>();
List<String> votes4 = new ArrayList<>();
votes4.add("A");
votes4.add("A");
votes4.add("A");
candidates1.add(votes4);
System.out.println(sortedWinner(candidates1));
System.out.println(sortedWinner(null));}
//voters * votes
public static List sortedWinner(List<List> candidates){
//List result = new ArrayList<>();
if(candidates==null || candidates.isEmpty()){
return new ArrayList<>();
}
//contain points of candidates
Map<String, Integer> map = new LinkedHashMap<>();//A-8
//voters*votes
for(List<String> candidate : candidates){
int len = candidate.size();
int points = 3;
for(int i=0; i<len; i++){
String candName = candidate.get(i);
map.putIfAbsent(candName, 0);
map.put(candName, map.get(candName)+points);//
points--;
}
}
System.out.println(map);
//voters*log(voters)
List<String> sortedWinner = map.keySet().stream().sorted((e1, e2)-> {
if(map.get(e1)!=map.get(e2)){
return map.get(e2) - map.get(e1);
}
return 0;
}).collect(Collectors.toList());
return sortedWinner;}
}
I got ** STRONG NO HIRE** for both thr rounds
I am seeking help from the community to review my solution to help where I went wrong
These are working code
Unable to understand the reason for ** STRONG NO HIRE**