Dropbox Interview Question
Software EngineersCountry: United States
Interview Type: Written Test
public class CountMethodCounter {
public static int TIME_DURATION = 10;
List<LocalTime> callList = new LinkedList<>();
public void inc(){
SimpleDateFormat sdf = new SimpleDateFormat("hh:MM:ss");
callList.add(LocalTime.now());
}
public void display(){
for(LocalTime localTime:callList){
System.out.println(localTime);
}
}
private boolean isWithinTime(LocalTime l1, LocalTime l2){
if(SECONDS.between(l1,l2)>TIME_DURATION){
return false;
}
return true;
}
public int count(){
int count=0;
LocalTime currentTime = LocalTime.now();
System.out.println("Count() called at ::"+currentTime);
for(LocalTime local:callList){
if(isWithinTime(local,currentTime)){
count=callList.size()-callList.indexOf(local);
break;
}else{
count=-1;
}
}
return count;
}
public static void main(String[] args) throws InterruptedException {
CountMethodCounter countMethodCounter = new CountMethodCounter();
System.out.println("Calling Methoods");
countMethodCounter.inc();
TimeUnit.SECONDS.sleep(10);
countMethodCounter.inc();
TimeUnit.SECONDS.sleep(10);
countMethodCounter.inc();
TimeUnit.SECONDS.sleep(10);
countMethodCounter.inc();
countMethodCounter.inc();
countMethodCounter.inc();
countMethodCounter.inc();
countMethodCounter.display();
System.out.println(countMethodCounter.count());
}
}
public class CountMethodCounter {
public static int TIME_DURATION = 10;
List<LocalTime> callList = new LinkedList<>();
public void inc(){
SimpleDateFormat sdf = new SimpleDateFormat("hh:MM:ss");
callList.add(LocalTime.now());
}
public void display(){
for(LocalTime localTime:callList){
System.out.println(localTime);
}
}
private boolean isWithinTime(LocalTime l1, LocalTime l2){
if(SECONDS.between(l1,l2)>TIME_DURATION){
return false;
}
return true;
}
public int count(){
int count=0;
LocalTime currentTime = LocalTime.now();
System.out.println("Count() called at ::"+currentTime);
for(LocalTime local:callList){
if(isWithinTime(local,currentTime)){
count=callList.size()-callList.indexOf(local);
break;
}else{
count=-1;
}
}
return count;
}
public static void main(String[] args) throws InterruptedException {
CountMethodCounter countMethodCounter = new CountMethodCounter();
System.out.println("Calling Methoods");
countMethodCounter.inc();
TimeUnit.SECONDS.sleep(10);
countMethodCounter.inc();
TimeUnit.SECONDS.sleep(10);
countMethodCounter.inc();
TimeUnit.SECONDS.sleep(10);
countMethodCounter.inc();
countMethodCounter.inc();
countMethodCounter.inc();
countMethodCounter.inc();
countMethodCounter.display();
System.out.println(countMethodCounter.count());
}
}
public class CountMethodCounter {
public static int TIME_DURATION = 10;
List<LocalTime> callList = new LinkedList<>();
public void inc(){
SimpleDateFormat sdf = new SimpleDateFormat("hh:MM:ss");
callList.add(LocalTime.now());
}
public void display(){
for(LocalTime localTime:callList){
System.out.println(localTime);
}
}
private boolean isWithinTime(LocalTime l1, LocalTime l2){
if(SECONDS.between(l1,l2)>TIME_DURATION){
return false;
}
return true;
}
public int count(){
int count=0;
LocalTime currentTime = LocalTime.now();
System.out.println("Count() called at ::"+currentTime);
for(LocalTime local:callList){
if(isWithinTime(local,currentTime)){
count=callList.size()-callList.indexOf(local);
break;
}else{
count=-1;
}
}
return count;
}
public static void main(String[] args) throws InterruptedException {
CountMethodCounter countMethodCounter = new CountMethodCounter();
System.out.println("Calling Methoods");
countMethodCounter.inc();
TimeUnit.SECONDS.sleep(10);
countMethodCounter.inc();
TimeUnit.SECONDS.sleep(10);
countMethodCounter.inc();
TimeUnit.SECONDS.sleep(10);
countMethodCounter.inc();
countMethodCounter.inc();
countMethodCounter.inc();
countMethodCounter.inc();
countMethodCounter.display();
System.out.println(countMethodCounter.count());
}
}
Looking for help on interview preparation?
Visit AONECODE.COM for ONE-TO-ONE private lessons by FB, Google and Uber engineers!
Customized course covers
System Design (for candidates of FB, LinkedIn, AMZ, Google and Uber etc)
Algorithms (DP, Greedy, Graph etc. every aspect you need in a coding interview and Clean Coding)
Interview questions sorted by companies
Mock Interviews
Our members got into G, U, FB, Amazon, LinkedIn, MS and other top-tier companies after weeks of training.
Feel free to email us aonecoding@gmail.com with any questions. Thanks!
SOLUTION:
Step 1, have a table to store all the visits sorted by time stamp.
or
have a queue to store the visits per second in the past 5 minute.
Follow-up:
Have two arrays hits[range] and lastupdated[range].
Range = 5 mins = 300 seconds in this case. Any hit within 'range' time from now is valid and should counted.
Index of a hit in the two arrays will be timestamp % 300.
Store the last updated time of a hit. Later if a query for the hit count arrives with a query timestamp, sum up all the valid hits from array 'hits'. Threshold for valid hits: query time - hit last updated time < 300.
Followup2
- aonecoding January 18, 2018For writing,
Concurrency update becomes an issue. Add write lock for protection. But this may slowdown the machine badly.
Move hit counters onto a distributed system. Have several machines counting together. Assign userIDs to diff hosts.
Add LB on top to make sure requests get distributed evenly.
Upon reading, sum up counts across all machines. For a read-heavy system, add cache.