Two Sigma Interview Question for Software Engineer / Developers

Country: United States
Interview Type: Phone Interview

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

I'm going to assume that the queues are sorted by timestamp. If not, then just sort each queue before starting for O(n log n).

General strategy is to peek the lowest element from both of the queues. Then peek the other queue for entries < 1 second away. If no elements such elements exist, dequeue the lowest element and try again.

I don't understand the 'one queue is slow' portion, I'll leave that unaddressed.

- cgj May 11, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
of 0 vote

Idea: We keep a buffer of elements from the fast queue that may be required in future pairs. We examine the items in the slow Queue one by one, we first "trim" off the elements of the buffer that can no longer be in an output pair, then we examine the items in the buffer and finally we pop new elements off of the fast queue if they form a pair and place them in the buffer.

We arrange the loops this way so the accesses to the "slow" queue are more spread out.

Complexity analysis, O(nm) where n and m are the number of elements in the slow and fast queues, respectively. We will get achieve this complexity when all time stamps and all queues are within one second, for example.

#include <vector>
#include <queue>
#include <cassert>
#include <deque>
#include <iostream>

using price_t = unsigned int;
using Time_t = double;

struct data_t
  price_t price;
  Time_t time;
  data_t(price_t p, Time_t t)
    : price {p}
    , time {t}

std::vector<std::pair<price_t, price_t> >
twoQueues(std::queue<data_t> & Qfast,
	  std::queue<data_t> & Qslow)
  std::vector<std::pair<price_t,price_t> > result;
  std::deque<data_t> buffer;
    auto head = Qslow.front();
    const auto time_min = head.time - 1.0;
    const auto time_max = head.time + 1.0;
    while(!buffer.empty() && buffer.front().time < time_min){
    for(const auto val : buffer){
      assert(val.time >= time_min);
      assert(val.time <= time_max);      
      result.emplace_back(head.price, val.price);
    while(!Qfast.empty() && Qfast.front().time <= time_max){
      const auto temp = Qfast.front();
      result.emplace_back(head.price, temp.price);
  return result;


  std::queue<data_t> Q1, Q2;

  data_t elem = {1,.11};

  elem = {23,.21};

  elem = {1000,.1};  
  elem = {2000,.2};  
  elem = {1234,100.0};
  const auto something = twoQueues(Q1,Q2);
  for(const auto& val : something){
    std::cout << val.first << ',' << val.second << std::endl;
  return 0;

- fred May 31, 2017 | Flag Reply

Add a Comment

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


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


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