Dropbox Interview Question for Software Engineers

Country: United States
Interview Type: Written Test

Comment hidden because of low score. Click to expand.
2
of 2 vote

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.

``````public class HitCounter {

int[] times, hits;
int timeRange;

/** Initialize your data structure here. */
public HitCounter(int range) {
times = new int[range];
hits = new int[range];
timeRange = range;
}

/** Record a hit.
@param timestamp - The current timestamp (in seconds granularity). */
public void hit(int timestamp) {
int idx = timestamp % timeRange;
if (times[idx] != timestamp) {
times[idx] = timestamp;
hits[idx] = 1;
} else {
++hits[idx];
}
}

/** Return the number of hits in the past 5 minutes.
@param timestamp - The current timestamp (in seconds granularity). */
public int getHits(int timestamp) {
int res = 0;
for (int i = 0; i < timeRange; ++i) {
if (timestamp - times[i] < timeRange) {
res += hits[i];
}
}
return res;
}
}``````

Followup2
For 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.

Comment hidden because of low score. Click to expand.
0
of 0 vote

public class CountMethodCounter {

public static int TIME_DURATION = 10;

public void inc(){
SimpleDateFormat sdf = new SimpleDateFormat("hh:MM:ss");
}

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());
}
}

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````public class CountMethodCounter {

public static int TIME_DURATION = 10;

public void inc(){
SimpleDateFormat sdf = new SimpleDateFormat("hh:MM:ss");
}

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());
}
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````public class CountMethodCounter {

public static int TIME_DURATION = 10;

public void inc(){
SimpleDateFormat sdf = new SimpleDateFormat("hh:MM:ss");
}

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());
}
}``````

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.