Adobe Interview Question for Computer Scientists


Country: United States




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

Above piece of code will not work on any system. following is the reason:

-> Single Processor : Lets assume Thread1 executes first line and found that "lock = 0" so it will go in to else condition. Before executing "lock =1" statement task switching takes place and another thread2 also found that "lock = 0". Thus at the same time 2 threads are in their critical section.

-> Multiprocessor system : Assume that 2 threads are executing same piece of code in parallel and both of them found that "lock =0" so both threads will enter into critical section.

Thus above code will not work on any system.

- ansh04 August 30, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It will work, but might not do what you think it does.

- Anonymous August 30, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I Agree with your explanation. But It will be random behavior.
Sometime it will work some time it will crash or sometime it will be in infinite waiting.

- kuber September 12, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

ansh04 is saying the same thing. The problem with the code is that the 'lock' if and else is not getting executed atomically (this atomic lock execution is provided by libraries like POSIX threads in C by the CAS - Compare and Swap function)

- nharryp November 05, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Issue:
wait() will not work.

Root Cause: :
wait() (and notify()) work on resources\objects not on threads. But in this piece of code you are calling wait() on this (thread instance) not on object\resource.

Solution:
In place of static int variable use a Object as a lock:
private static final Object lock = new Object();

wait() -> lock.wait();

synchronized (lock) {
	lock.wait(); // It's already locked so wait(sleep) till someone wakes me up 
	try{
		/* Critical Section - Increment g */
	}finally{
		lock.notify();
	}
}

- Ajeet August 22, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 votes

This concept is not specific to java, it is a general rule for all systems or languages.

- Ajeet August 23, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is not specific to java..Its a general block of code

- rahul23111988 August 22, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

the lock itself is not thread safe, will not work in any system.

- Anonymous August 23, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The waked thread doesn't lock the lock before entering the critical section.

- Aickson August 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

first problem:
if one of the thread dies without unlocking no one can lock the resource again.
Second problem:
Only one thread can lock the resource at any point of time. Something like binary semaphore even on a multiprocessor system i.e. this code doesn't utilizes the mutlicore architecture.
As of now i can see only these two but i am sure there are many more.

- aka August 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The code will not work on any system. "if" should be replaced by "while" for the lock to serve its purpose.

- Namita September 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

* Time taken to execute lock can cause problems. Two or more threads can execute lock at same time. Better solutions is to use condition variables with mutex locks.

- Gagandeep September 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

To be precise, answer should be "it may work sometime and fail sometime"
Explanation:-
We have two processes P1 and P2, and lock=0;
if (lock) ---- Line 1
wait(); // It's already locked so wait(sleep) till someone wakes me up --- Line2
else ------ Line 3
lock=1; // I locked it ------ Line 4
/* Critical Section - Increment g */

lock = 0; // Lock released, so wakeup only one of other waiting threads, if any

Let's say P1 is scheduled first, and it execute Line1, and context switched with P2, and again P2 executes Line1, now we arrived to the condition where both P1 and P2 see lock as free, and will execute Line4, and as a result there will be deadlock as both will wait for lock to be freed but none would be able to do so.

- skum June 22, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It depends upon the scheduler.. To avoid this dependency we have to ensure that the lock value assignment is an atomic operation ..after that it will work fine in any conditions

- Dharma August 04, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

ash04 and others are right. This will NOT work. For those who mention working sometimes I have a financial program for you to use. Half the time it puts your paycheck in your bank account and the other half it puts it in mine ;-)

- erik_the_read February 09, 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