Interview Question for Senior Software Development Engineers


Country: India
Interview Type: In-Person




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

queue data structure is used

- jaya May 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Threads must be having some attribute(s). For example, resource , availability , ownership etc. Based on them we can have a priority among the threads set.

And based on priority we can put them in a heap. So whenever a request comes, we take the top element from the heap and assign it. And later re-arrange the heap.

- Vifi May 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

hashMap

- onyas May 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Short answer is a "synchronized queue".

If you look at the Java's Thread Pool implementation, I am talking about the java.util.concurrent package specifically, the thread pool Executors uses synchronized queues that implement the BlockingQueue<E> interface and the BlockingQueue<E> interface extends the generic java.util.Queue<E> interface.

You can look at the javadoc and see how the whole system is designed and not reinvent the wheel. Basically your thread pool implementation will use [Abstract]Factory pattern (see ThreadPool, Executor, ExecutorService, ThreadPoolExecutor classes) to create and execute your tasks and your tasks needs to implement the Java Runnable interface which is the Command design pattern. So you can use these design patterns along with a synchronized queue to come up with a Thread Pool implementation in any language of your own.

- math.matt May 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It depends a lot of the requirements for the thread pool. List, queue even stack, are all conceivable. What do you mean by 'assign a thread'?

- EugenDu May 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Assign means picking a thread from thread pool. Identifying the which thread id is free so we can use it by traversing the complete pool or by some look up method or we creating two separate list of free/busy or some marking of free/busy is there array/queue based pool.

- sumit kumar May 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

HashMap<Integer,ArrayList<Threads>>

1-->list of un allocated threads
2-->list of allocated threads

- rajdeeps.iitb August 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I might opt to go in with Priority Queue or per se Heap (note these two things are usually the same as the former can be implemented with Heap Data Structure, and still provide good space and time complexity. Not necessary implement in linked-list way to have full graph representation, but vector or even fixed-array is suffice).

Ok, back to decision of choosing Priority Queue (I say Heap from now).

It serves the following requirement
1. good time complexity when I need a new idle thread to be grabbed and used. We can achieve it with O(1) at the root
2. able to get the most prioritized thread from the pool to use. This most prioritized thread can be associated with the largest priority value, thus making 1. possible. When we grabbed such thread, Heap will update itself and 2nd best prioritized thread will be ready sitting tight for next time. Whenever we're done with executing the current thread, we can decrement its priority value, so when it gets back to the Heap it won't be at the root node, but lower thus give other threads chance to be selected freely with less effort.
3. We might not have to maintain busy list of threads. After each thread done its job, just push it into Heap. The cost will be N*logN wheres N is number of idle threads at that time.

Trade-off, just one thing I can think of that we miss from going with Heap according to above reasoning
1. We have no control over busy threads in execution. We cannot query info about it during run-time, only wait until it's done.

There's no right or wrong, but depends on your use case, requirement.

- Wasin September 22, 2019 | 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