Bloomberg LP Interview Question for Financial Software Developers


Team: R&D
Country: United States
Interview Type: In-Person




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

const int x;
const 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;
}

- figogo January 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

well actually I get it...my answer wont always return calls in last t seconds...

- Anonymous February 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Sandeep February 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Sandeep February 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// 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     }

- Vick May 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 3 vote

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

- Anonymous October 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

> Pretty self evident.

You missed the O(1) solution, genius.

- Anonymous October 27, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Where is O(1) mentioned ??

- Anonymous October 28, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

each time when the function is called,remove all those expire call from the beginning of the vector

- anonymous October 28, 2011 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

> 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).

- Anonymous October 28, 2011 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

When I said pretty self evident.. I meant the code that I posted not my own intelligence. People seem to get upset when others post code without explanation.

The way the question was posted it wasn't clear if x and n were constants for the life of the program.

- Anonymous October 29, 2011 | Flag
Comment hidden because of low score. Click to expand.
-1
of 3 vote

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?

- juan.jose.martin.marcos.gomez August 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- Vick August 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Now I get it.

Thanks, Vick.

- juan.jose.martin.marcos.gomez August 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 vote

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;

- Anonymous February 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

to edit my last answer, since a constant x is given and true or false answer is desired, last step would be to return count > x

- Anonymous February 08, 2012 | 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