Bloomberg LP Interview Question for Senior Software Development Engineers


Country: United States
Interview Type: Written Test




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

import static javafx.scene.input.KeyCode.T;

public class ProducerConsumerDemo {
    int value;
    volatile boolean flag = false;

    public synchronized int consume() {
        if (!flag) {
            try {
                wait();
            } catch (InterruptedException interruptedException) {
                interruptedException.printStackTrace();
            }
        }
        System.out.println("Consumed: " + value);
        flag = false;
        notify();
        return value;
    }

    public synchronized void produce(int value) {
        if (flag) {
            try {
                wait();
            } catch (InterruptedException interruptedException) {
                interruptedException.printStackTrace();
            }
        }
        this.value = value;
        flag = true;
        System.out.println("Produced: " + value);
        notify();
    }

    private class ProducerThread extends Thread {
        public void run() {
            for (int i = 0; i < 5; i++) {
                produce(i);
            }
        }
    }

    private class ConsumerThread extends Thread {
         public void run() {
             for (int i = 0; i < 5; i++) {
                 consume();
             }
         }
    }
    public static void main(String[] args) {
        ProducerConsumerDemo demo = new ProducerConsumerDemo();
        Thread t1 = demo.new ProducerThread();
        t1.start();
        Thread t2 = demo.new ConsumerThread();
        t2.start();
    }
}

- noname June 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

hum, it's a parallel computing pattern. in it's most primitive way, it's a semaphore, that is released by the producer and waited for by the consumer. Then, a bit more sophisticated would be to do the same with a variable that carries some information. Next level would be with a buffer in between and then the interesting questions come along, what if the buffer is full, etc.

- Chris June 19, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Is it possible to elaborate more on this question?

- JustYourAverageDev June 19, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <thread>
#include <condition_variable>
#include <mutex>
#include <iostream>
#include <vector>
#include <unistd.h>

using namespace std;

vector<int> buffer;

uint64_t produced_count = 0;
uint64_t consumed_count = 0;
mutex counts_mutex;
condition_variable counts_cv;

void producer()
{
	while (true) {
		unique_lock<mutex> counts_lock(counts_mutex);
		while (produced_count - consumed_count == buffer.size()) {
			counts_cv.wait(counts_lock);
		}
		
		int product = rand();
		buffer[produced_count++ % buffer.size()] = product;
		cout << "produced: " << product << "\n";
		
		counts_cv.notify_all();
	}
}

void consumer()
{
	while (true) {
		unique_lock<mutex> counts_lock(counts_mutex);
		while (produced_count - consumed_count == 0) {
			counts_cv.wait(counts_lock);
		}
		
		int product = buffer[consumed_count++ % buffer.size()];
		cout << "consumed: " <<  product << "\n";
		
		counts_cv.notify_all();
	}
}

int main(int argvc, char const **argv)
{
    buffer.resize(16, 0);
	// to handle the uint64_t counts overflow, the following must be true:
	// (0xffffffffffffffff + 1) % buffer.size() == 0
 
    vector<thread> threads;
    for (int i = 0; i < 3; ++i) {
        threads.push_back(thread(producer));
    }
	sleep(3);
    for (int i = 0; i < 3; ++i) {
        threads.push_back(thread(consumer));
    }
    for (int i = 0; i < threads.size(); ++i) {
        threads[i].join();
    }
    return 0;
}

- Alex August 13, 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