Dropbox Interview Question for Software Engineers


Country: United States
Interview Type: In-Person




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

#include <ctime>
class DropBoxHitRecord{
	public:
	static int ID;
	static map<time_t, unsigned int> Hit;
	void recordHit(){
		ID++;
		Hit.insert(std::make_pair(time(0), ID));
	}

	long getCount(){
		long count = 0;
		std::map<time_t, unsigned int>::iterator It;
		It = Hit.lower_bound(time(0)- 300);
		for (; It != Hit.end(); ++It){
			count++;			
		}
		return count;
	}

};

- Uchman21 April 27, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

List<Long> hitTime = new LinkedList<Long>();
	
	void recordHit(){
		
		hitTime.add(System.currentTimeMillis());
	}

	int getCount(){
		
		long startTime = System.currentTimeMillis() - 30 * 1000;
		int cnt = 0;
		ListIterator<Long> it = hitTime.listIterator(hitTime.size());
		while(it.hasPrevious()){
			if(it.previous() > startTime)
				cnt++;
			else
				break;
		}
		return cnt;
	}

- Madhukar April 27, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

In java

1. Initialize queue for dateTime class.
2. Maintain a counter to track number of elements in queue at given time.
3. For recordHit(), make current timestamp entry in queue (BlockingQueue in java).
4. For getCounter(), keep dequeue expired values from queue. return the counter.
5. Create a timertask in java which runs in separate thraed and keep queue in sync by removing expired entries.

- jugaad April 28, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package designQs;

import java.util.Date;
import java.util.HashMap;

public class CareerCupDropBox {
	
	static Integer ID = 0;
	static HashMap< Date , Integer> hitMap = new HashMap<Date , Integer >();
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		

	}
	
	public static void recordHit()	{
		//ID++;
		hitMap.put(new Date(), ++ID);
	}
	
	public static int getCount(){
		Date date = new Date();
		return ID - hitMap.get(new Date( date.getTime() - 300000)) ;
	}

}

One thing to be considered is the synchronized use of static variable ID is important

- Sibendu Dey April 27, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package com.madhukar.test;

import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

public class CarrerCupDBTest {
	
	List<Long> hitTime = new LinkedList<Long>();
	
	void recordHit(){
		
		hitTime.add(System.currentTimeMillis());
	}

	int getCount(){
		
		long startTime = System.currentTimeMillis() - 5 * 60 * 1000; // 5 minutes back time
		int cnt = 0;
		ListIterator<Long> it = hitTime.listIterator(hitTime.size());
		while(it.hasPrevious()){
			if(it.previous() > startTime)
				cnt++;
			else
				break;
		}
		return cnt;
	}
	
	public static void main(String[] args) {
		
	}

}

- Madhukar April 27, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

long hit=0;
long count;
long hitStartTime = System.currentTimeMillis();
long currTime=0;
Calendar calendar = Calendar.getInstance();

public void recordHit(){
calendar.setTime(new Date());
if(hit==0){

hitStartTime = calendar.get(Calendar.MINUTE);
}

hit++;
count=hit;
if(calendar.get(Calendar.MINUTE)-hitStartTime==5){
count =0;
hitStartTime = calendar.get(Calendar.MINUTE);
}
}
public long getCount(){

return count;
}

- vishal April 27, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

long hit=0;
    long count;
    long hitStartTime = System.currentTimeMillis();
    long currTime=0;
    Calendar calendar = Calendar.getInstance();
    
    public void recordHit(){
    	calendar.setTime(new Date());
    	if(hit==0){
        	
    		hitStartTime = calendar.get(Calendar.MINUTE);
    	}
    	
    	hit++;
    	count=hit;
    	if(calendar.get(Calendar.MINUTE)-hitStartTime==5){
    		count =0;
    		hitStartTime = calendar.get(Calendar.MINUTE);
    	}
    }
    public long getCount(){
    	
    	return count;
    }

- vishal April 27, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

long hit=0;
    long count;
    long hitStartTime = System.currentTimeMillis();
    long currTime=0;
    Calendar calendar = Calendar.getInstance();
    
    public void recordHit(){
    	calendar.setTime(new Date());
    	if(hit==0){
        	
    		hitStartTime = calendar.get(Calendar.MINUTE);
    	}
    	
    	hit++;
    	count=hit;
    	if(calendar.get(Calendar.MINUTE)-hitStartTime==5){
    		count =0;
    		hitStartTime = calendar.get(Calendar.MINUTE);
    	}
    }
    public long getCount(){
    	
    	return count;

}

- vishal April 28, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

long hit=0;
    long count;
    long hitStartTime = System.currentTimeMillis();
    long currTime=0;
    Calendar calendar = Calendar.getInstance();
    
    public void recordHit(){
    	calendar.setTime(new Date());
    	if(hit==0){
        	
    		hitStartTime = calendar.get(Calendar.MINUTE);
    	}
    	
    	hit++;
    	count=hit;
    	if(calendar.get(Calendar.MINUTE)-hitStartTime==5){
    		count =0;
    		hitStartTime = calendar.get(Calendar.MINUTE);
    	}
    }
    public long getCount(){
    	
    	return count;

- vishal April 28, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

long hit=0;
    long count;
    long hitStartTime = System.currentTimeMillis();
    long currTime=0;
    Calendar calendar = Calendar.getInstance();
    
    public void recordHit(){
    	calendar.setTime(new Date());
    	if(hit==0){
        	
    		hitStartTime = calendar.get(Calendar.MINUTE);
    	}
    	
    	hit++;
    	count=hit;
    	if(calendar.get(Calendar.MINUTE)-hitStartTime==5){
    		count =0;
    		hitStartTime = calendar.get(Calendar.MINUTE);
    	}
    }
    public long getCount(){
    	
    	return count;
    }

- vishal April 28, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It is very good question as I cannot see a good/generic solution with natural limit on the resource (container's memory) while extending the parameter (get recordHit for last 5min, 5 hours, 5 years...). If there is no limit on the resource OR we presume the number of hits for the 5 min is much less than available bytes in memory, I would use (in C++/STL notation with abstract time_t type) the deque<time_t> container (called myDeque below) with a helper function to remove expired times:

void RemoveOld(time_t tm, deque<time_t>& dqTimes) {	
 	while( !dqTimes.empty() && dqTimes.front() < tm )
		dqTimes.pop_front();	
}
void recordHit() {
	time_t tm = --CurrentTime--;
	RemoveOld(tm - 5 Min, myDeque);
	myDeque.push_back(tm);
}
long getCount() {
	time_t tm = --CurrentTimeMinus5Min--;
	RemoveOld(tm, myDeque);
	return myDeque.size();
}

- celicom May 07, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import queue
import datetime
import time


q = queue.deque()


def recordHit():
tm = datetime.datetime.utcnow()
tm5 = tm - datetime.timedelta(seconds=5)
print (tm)
q.appendleft(calculatetimeinmili(tm))
removeold(calculatetimeinmili(tm5))
getCount()


def getCount():
print (len(q))


def removeold(time):
while len(q)> 0 :
tmptime = q.pop()
if tmptime > time:
q.append(tmptime)
break


def calculatetimeinmili(tm):
return (tm.hour * 3600 + tm.minute * 60 + tm.second)*1000000 +tm.microsecond


recordHit()
time.sleep(3)
recordHit()
time.sleep(1)
recordHit()
time.sleep(3)
recordHit()
time.sleep(6)
recordHit()

- lior April 29, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I would just use a circular buffer

#include <time.h>
#include <mutex>

#define NUM_BUCKETS 512

struct Bucket {
	time_t time {0};
	long count {0};
};

Bucket bucketedCounts[NUM_BUCKETS];
std::mutex mtx;

void recordHit() {
	time_t curTime;
	time(&curTime);
	auto idx = (curTime % NUM_BUCKETS);
	std::lock_guard<std::mutex> lock(mtx);
	auto& count = bucketedCounts[idx].count;
	if (bucketedCounts[idx].time != curTime) {
		bucketedCounts[idx].time = curTime
		count = 1;
	} else {
		++count;
	}
}

long getCount() {
	time_t curTime;
	time(&curTime);
	int curIdx = (curTime % NUM_BUCKETS);
	long numHits = 0;
	{
		std::lock_guard<std::mutex> lock(mtx);
		if (bucketedCounts[curIdx].time == curTime) {
			numHits += bucketedCounts[curIdx].count;
		}
	}
	for (time_t checkTime = curTime - 300; checkTime < curTime - 1; ++checkTime) {
		int idx = (checkTime % NUM_BUCKETS);
		if (bucketedCounts[idx].time == checkTime) {
			numHits += bucketedCounts[idx].count;
		}
	}
	return numHits;
}

- AL May 17, 2017 | Flag Reply


Add a Comment
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.

Learn More

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.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More