Click here to Skip to main content
15,798,825 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
One of the interview questions asked is as follows

Consider a doubly linked list with each node having a send(Integer) method.
Different instances of same threads are running in them. How would you implement the run method of the thread class so that each node prints the sum of complete linked list. Please Note: Each Node can talk to its neighbours only

What I have tried:

public class LinkedListNode {
LinkedListNode next;
LinkedListNode previous;
int value;

public int send(int value){
this.value = value; = null;
this.previous = null;
Updated 19-Feb-16 19:38pm
Sergey Alexandrovich Kryukov 19-Feb-16 23:31pm    
All right, you failed the interview. So what? If someone writes this piece of code for you, you are going to fail next interview. You really need to develop your own skills. Looking at the solutions written by others cannot help you at all. You will know how to solve nest problem only if you solve some representative set of different problems, all by yourself.
BillWoodruff 20-Feb-16 23:44pm    
What's your question ? Did the interview question ask you to write code or just explain a strategy.

"Different instances of same threads are running in them." This is probably incorrect: I suspect you (they ?) were talking about one instance of a linked list being accessed by multiple concurrent threads.

"having a send(Integer) method" ... which does ... what ?
Member 11975188 21-Feb-16 0:07am    
They asked me to write a code by giving this explanation.

I have a knowledge on Multithreading, but never got a chance to implement in any of my webforms projects.

1 solution

Please see my comment to the question.

Sorry, it needs time to write solution which would not help you much anyway. So, just a few notes will be more useful:

"Different instanced of same threads" is absurd. If instances are different, they are not same threads at all. From the other hand, different threads can run the same function, or the also the same function on the same "this" object, but the "same function" has little significance for threads. However, same "this" is important, because then the threads get access to the same, shared object, so the problem becomes the issue of thread synchronization. You need to learn it:
Synchronization (computer science) — Wikipedia, the free encyclopedia[^],
Overview of Synchronization Primitives[^],
Thread Synchronization (C# and Visual Basic)[^].

See also: Mutual exclusion — Wikipedia, the free encyclopedia[^].

Perhaps you simply fail to explain the problem properly, and the real problem related to what you called "different instances" meant to be different threads working with some shared objects.

Sorry to say that, but I don't really expect you to really understand all of the above, because you, for example, did not even try to create any thread in your code sample. And you did not even try to create a linked list, because you, essentiall, did not even try to link any of your nodes. This is much easier when you work with .NET references and hence GC takes care of lost references. Probably, you first need to learn the very basics of programming in general and not prepared for the interviews. As to the threading, you need to learn it only if you learn all the prerequisites. Then you can get to threding:
Multithreading (computer architecture) - Wikipedia, the free encyclopedia[^],
Thread (computing) — Wikipedia, the free encyclopedia[^],
Parallel computing — Wikipedia, the free encyclopedia[^],
Managed Threading[^],
Threading Tutorial (C#)[^],
System.Threading Namespace[^].

And no, it's useless to try to prepare to interviews and especially to look through some available interview answers and test problem solutions. You really want to study the subject on regular basis and do a lot of the real job to be ready to get hired. For some ideas, please see my past answer: SSRS interview questions and answers[^].

Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900