Bloomberg LP Interview Question
Financial Software DevelopersTeam: R&D
Country: United States
Interview Type: In-Person
#include <iostream>
#include <stdio.h>
#include <ctime>
using namespace std;
#define x 5
#define t 5
bool func_times_called()
{
static time_t visits[x-1];
static int current = 0;
time_t oldest_visit = visits[current];
time_t now = time (NULL);
visits[current] = now;
current = (current + 1)%(x-1);
if (oldest_visit == 0)
return false;
if (difftime(now, oldest_visit) < t)
return true;
else
return false;
}
int main()
{
while(1)
{
cout<<" Value = "<<func_times_called()<<endl;
getchar();
}
}
#include <iostream>
#include <stdio.h>
#include <ctime>
using namespace std;
#define x 5
#define t 5
bool func_times_called()
{
static time_t visits[x-1];
static int current = 0;
time_t oldest_visit = visits[current];
time_t now = time (NULL);
visits[current] = now;
current = (current + 1)%(x-1);
if (oldest_visit == 0)
return false;
if (difftime(now, oldest_visit) < t)
return true;
else
return false;
}
int main()
{
while(1)
{
cout<<" Value = "<<func_times_called()<<endl;
getchar();
}
}
// How about a sprinkle of C++11 just for fun
74 typedef chrono::monotonic_clock::time_point timepoint_t;
75 typedef chrono::monotonic_clock::duration duration_t;
76 typedef list<timepoint_t> times_t;
77
78 const timepoint_t delta(chrono::seconds(5)); // 'x'
79 const size_t limit = 10; // 't'
80
81 bool decorator(void)
82 {
83 static size_t count;
84 static times_t times;
85
86 timepoint_t now = chrono::monotonic_clock::now();
87 timepoint_t tail(now - delta);
88
89 // Update globals with this hit
90 times.push_back(now);
91 count++;
92
93 // Trim expired hits
94 for (times_t::iterator i = times.begin(); i != times.end(); )
95 {
96 if (*i > tail) break;
97
98 count--;
99 i = times.erase(i);
100 }
101
102 return (count == limit);
103 }
Pretty self evident.
Store all calls in an array of vectors. Do a binary search and return count
#include <time.h>
#include <vector>
std::vector<clock_t> Calls;
bool WasCalled( int x , int t )
{
bool wasCalled = false;
clock_t currentTime = clock();
clock_t oldTime = currentTime - t * CLOCKS_PER_SEC;
std::vector<clock_t>::const_iterator iter = std::lower_bound( Calls.begin() , Calls.end() , oldTime );
if ( iter != Calls.end() )
{
int numCount = Calls.end() - iter;
if ( numCount == x )
wasCalled = true;
}
Calls.push_back( currentTime );
return wasCalled;
}
each time when the function is called,remove all those expire call from the beginning of the vector
> Where is O(1) mentioned ??
It's not, but if you're going to make cheeky self-congratulatory comments, you'd better get the best answer (store the last x-1 invocations in a circular queue, check whether the entry you're about to overwrite was more than t seconds ago).
I don't get to understand why you guys use a vector or an array, when we only need to count how many times the function is called and when was the first one inside the last "t" seconds. Something like this (pseudocode):
define x, t
bool foo()
{
static count = 1
static begin = now
currentTime=now
if (currentTime-begin > t)
{
count = 1
begin = now
}
else
{
++count
}
if (count >= x)
return true
else
return false
}
This is O(1), isn't it?
By resetting count to 1 aren't you losing all the hits that happened in the last t seconds? 't' is a sliding window and not a box hence the need to remember when each hit happened. Hence the vector.
Why wouldn't something like this work ?
Store only two values : timestamp and an int count.
int wasCalled()
{
int curr_timestamp = getcurrent_timestamp(); // need to look up the exact function.
if (timestamp == 0)
{
timestamp = curr_timestamp;
count ++;
}
else if ((curr_timestamp - timestamp) > t)
{
// More than t seconds elapsed
timestamp = curr_timestamp;
count = 1;
}
else
{
// Within t seconds
count++;
}
return count;
const int x;
- figogo January 03, 2013const int t;
bool have_we_made_enough_calls_lately() {
static deque<int> times;
times.push_back(current_time_in_msec());
if (times.size() > x)
times.pop_front();
return times.back() - times.front() <= 1000 * t;
}