Threads Interview Questions
- 0of 0 votes
AnswersFollowing code is used by ONE producer and ONE consumer
- Karrie November 26, 2013 in India
public void Produce(queue<int> queue, ManualResetEvent mre){
while(true){
lock(queue){
queue.enque(3);
}
mre.set();
}
}
public void Consume(queue<int> queue, ManualResetEvent mre){
while (true){
mre.Reset();
if (queue.Count == 0)
mre.WaitOne();
lock(Queue){
var x = queue.Dequeue();
}
}
}
the last code line causes error, queue is empty-
"var x = queue.Dequeue();"
they asked me to suggest a fix for that.| Report Duplicate | Flag | PURGE
IBM Software Engineer / Developer Threads - 0of 0 votes
AnswersThere are two methods printA inside Class A and printB inside class B. printA is a static method and printB is a non-static method. Both the methods are synchronized. There exact 100 million threads fired on both printA and printB each. Which method execution will take less time?
- techpanja November 21, 2013 in United Statespublic class A { public static void synchronized printA() { ...////..... } } public class B { public void synchronized printB() { ...////..... } }
| Report Duplicate | Flag | PURGE
Amazon Software Engineer / Developer Threads - 0of 2 votes
AnswersWhere could we use deadlock? The interviewer was very clear about his question. The use of deadlock.
- Ram August 06, 2013 in India for QATeam
In which scenario could we use deadlock?| Report Duplicate | Flag | PURGE
VMWare Inc Quality Assurance Engineer Threads - 0of 0 votes
AnswersI have to create 5 threads where each thread has to perform the addition operation.
- Ram August 06, 2013 in India for QATeam
Thread1 - Add 1 to 10
Thread2 - Add 1 to 50
Thread3 - Add 5 to 15
Thread4 - Add 10 to 20
Thread5 - Add 15 to 20
What is the best way to accomplish this?
Also, I need 1 sec time delay between each addition operation.| Report Duplicate | Flag | PURGE
VMWare Inc Quality Assurance Engineer Threads - 1of 1 vote
AnswersDifference between concurrency and parallelism
- JSDUDE June 27, 2013 in United States
How has threading helped concurrency and parallelism?| Report Duplicate | Flag | PURGE
Ebay SDE1 Threads - 1of 1 vote
AnswerFormula for nth PI is (-1^n)/(2n+1). Write an efficient way to calculate this.
- JSDUDE June 27, 2013 in United States
After i wrote a recursive and iterative solution, she wanted a faster solution. So i went to threading.
Eventually she said that if the value of n is a million and you can't have million threads how will you distribute:
Problem is as n increases the above computation becomes time consuming and hence cannot be simply split in ranges.
She hinted me towards: Queue of jobs, threads picking jobs and en-queuing the work| Report Duplicate | Flag | PURGE
Ebay SDE1 Math & Computation Threads - -5of 7 votes
Answersneed to implement a weather report functionality. user will provide the city name , need to return the weather report.
- gopi.komanduri May 29, 2013 in India
if weather station exists n functioning properly , will return the weather report of that station .
else ,
will return the nearest available weather station report.
interviewer looking for optimized manner.
looking for datastructures to stores the cities n algo to return the report.| Report Duplicate | Flag | PURGE
Mentor Graphics Analyst Algorithm Arrays Bit Manipulation Brain Teasers C C# C++ Cache Coding Computer Architecture & Low Level Data Mining Data Structures Dynamic Programming General Questions and Comments Graphics Hash Table Ideas Linked Lists Math & Computation Object Oriented Design Problem Solving Sets Sorting Stacks String Manipulation Terminology & Trivia Threads Trees and Graphs XML - 1of 1 vote
AnswersIf two threads are incrementing a variable 100 times each without synchronization, what would be the possible min and maximum value.
- Huwanda May 10, 2013 in United States| Report Duplicate | Flag | PURGE
VMWare Inc Member Technical Staff Threads - 0of 0 votes
AnswersSingle Initialization :
- JSDUDE May 04, 2013 in United States
Global variable x, initialized to 0.
Implement a function that can be called by multiple threads simultaneously or sequentially.
The value of x should be set to the current time only once. If it is already set, the value shouldn't be updated.
Make sure that the function doesn't become a bottleneck| Report Duplicate | Flag | PURGE
Microsoft SDE1 Threads - 0of 0 votes
AnswerThere is a HealthMonitor and two Servers (Primary and Secondary), all connected to one and another.
- JSDUDE May 04, 2013 in United States
The HealthMonitor keeps pinging both the servers at specific time intervals and waits for their response for a time-out period after the request has been sent.
The server responds with a health status of itself and of its neighbor (meaning Primary responsds: OK; NEIGHBOR_OK)
Implement the server's code to send and receive responses and then take action based on response.| Report Duplicate | Flag | PURGE
Amazon SDE1 Threads - 0of 0 votes
AnswersHow could you make sure that thread A ,B and C run sequentially without using join method?
- cCAACc May 04, 2013 in United States| Report Duplicate | Flag | PURGE
Barclays Capital Senior Software Development Engineer Threads - 0of 0 votes
AnswersSay there are 3 array lists l1, l2 & l3 of same length. Thress threads accessing three lists. Say T1 -> l1, T2 ->l2 & T3 ->l3. It should print in the order say first element of 1st then first element of 2nd list and then first element of 3rd list. Then second element of 1st then second element of 2nd list and then second element of 3rd list.
- rizwan.amd March 26, 2013 in India| Report Duplicate | Flag | PURGE
Novell Tech Lead Threads - 3of 3 votes
AnswersCoding:
Public void TransferAccount(AccountID id1, AccountID id2){ Account a1 = id1.GetAccount(); Account a2 = id2.GetAccount(); //Swap amounts. Temp = a1.Balance; a1.Balance = a2.Balance; a2.Balance = Temp; }
Q1: How do you make it thread safe?
I said use “public void synchronized” Good. But terrible performance since the entire method is synchronized.
Q2: Can you not lock on the entire method? I said used nested locks:Synchonized(a1) Synchronized(a2) { //swap }
His q: This will lead to a deadlock if in another thread I call Transfer (id2, id1) and Transfer (id1, id2).
Synchonized(a1) Synchronized(a2) { //swap }
Synchonized(a2) Synchronized(a1) { //swap }
How do you prevent this then? How do you design your code to not to get in to deadlock? (stumbled here)
- xankar March 16, 2013 in United States| Report Duplicate | Flag | PURGE
Amazon Software Engineer / Developer Algorithm Java Threads - 0of 0 votes
AnswersHow do you detect deadlocks? What tools would you use? I said do “Kill -3 .<process id>, and analyse if anything is deadlocked.
- xankar March 16, 2013 in United States| Report Duplicate | Flag | PURGE
Amazon Software Engineer / Developer Java Threads - 0of 0 votes
AnswersCoding: Write a Client/Server. Three methods are given. Msg.Get(), Msg.Process(), Msg.Send(). Write code. Since Msg.Get() and Msg.Send() has to send messages over the network. It takes a lot more number of threads. So how many threads out of 10, would allocate to each of the three processes. What is the proportion?
- xankar March 16, 2013 in United States| Report Duplicate | Flag | PURGE
Amazon Software Engineer / Developer Threads - 0of 2 votes
AnswersHow would you implement call for canceling queued blocks with dispatch_after?
- peetonn March 09, 2013 in United States| Report Duplicate | Flag | PURGE
Facebook iOS Developer Threads - 0of 2 votes
AnswersDifference between thread and process
- abcd March 06, 2013 in United States| Report Duplicate | Flag | PURGE
Ebay Software Engineer / Developer Threads - 0of 0 votes
AnswersHow would a mutex lock be implemented by the system?
- andrew February 05, 2013 in United States| Report Duplicate | Flag | PURGE
Bloomberg LP Financial Software Developer Threads - 0of 0 votes
AnswersWhat will be the minimum and maximum output of I
- sach.m December 17, 2012 in United Statesi=0; threadA { i++; i++; i++; } threadB { i++; i++; i++; i++; i++; }
| Report Duplicate | Flag | PURGE
Threads - 0of 0 votes
AnswersJAVA Question:
- SK October 28, 2012 in United States
There is one class which extends Thread and it takes "Symbol (Clubs, Heart, Dice, Spade)" as a parameter in constructor.
We've four threads one each for each symbol. Now they want to print as given below:
Dice A
Hearts A
Spade A
Clubs A
Dice 2
Hearts 2
Spade 2
Clubs 2
and so on...
How to achieve this. I thought of using wait/notify, but we've to some how make sure that each thread gets run in a particular order to print this.| Report Duplicate | Flag | PURGE
Threads - 1of 1 vote
AnswersDesign a class (or several classes) to be used to implement C++ Thread Management. You may use any
underlying thread library you choose (POSIX pthreads, Windows Threads, or some other open-source or
easily accessible threading library). Ideally your Threads should give me a safe method to pass progress
back to the main thread on the progress of the execution or my work that I’ve decided to perform with
your thread.
If something is unclear please note what assumptions you are making.
The following is an example of a test program that would use your Thread class
// I've created a thread that does some work and inherits from your base Thread class
- therani.arch October 24, 2012 in United Statesclass DoWorkThread : public <YourThreadClass> {...}; int main(){ ... // DECLARE the base class DoWorkThread example_worker ( data ); // Start the thread example_worker.start(); ... <other calls you define that I would use> }
| Report Duplicate | Flag | PURGE
Riverbed Software Engineer / Developer Threads - 0of 0 votes
AnswerWhat is a Executer in threads??
- sarthakiter July 17, 2012 in India| Report Duplicate | Flag | PURGE
IBM Developer Program Engineer Threads - 1of 1 vote
AnswersWrite a multithreaded blocking version of Bounded Queue.
- Anon June 21, 2012 in United States for systems and infrastructure
There are writers which fill up the queue, and then readers which empty the queue.
If q is full, writer should block. If Q is empty readers should block.
The Queue size is finite... (its bounded).
Maximize parallelism, minimize resource usage etc...| Report Duplicate | Flag | PURGE
Linkedin Threads - 0of 0 votes
AnswersYou are given a paragraph , which contain n number of words, you are given m threads. What you need to do is , each thread should print one word and give the control to next thread ... this way each thread will keep on printing one word , in case last thread come, it iwill invoke the first thread ... procedure will repeat until all the words are printed in paragraph. Finally all the thread should exit gracefully. What kind of synchronization will use ? Answer only the logic to implement (not complete code)
- Interviews January 29, 2012 in India| Report Duplicate | Flag | PURGE
Amazon Software Engineer / Developer Threads - 0of 0 votes
Answerswhat do threads share amongst themselves and what they dont share?Please help me in the ans.I know Heap is shared b/w threads and stack is not shared.What els do the have them in common?
- tejaswibm January 06, 2012 in India| Report Duplicate | Flag | PURGE
Software Engineer / Developer Threads - 0of 0 votes
Answerswhat is the advantage of using threads in case of single processor system, if the alternative single thread process is an interactive one i.e. it is responsive throughout its execution.
- neha9.possible December 30, 2011 in United States| Report Duplicate | Flag | PURGE
The Royal Bank of Scotland Chennai Software Engineer / Developer Threads - 0of 0 votes
AnswersWhat is critical section ?
- lipun4u December 03, 2011 in India| Report Duplicate | Flag | PURGE
Manhattan associates Developer Program Engineer Threads