Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

One important thing to question the interviewers at this point is, for a particular class, say A, are all the calls of method printA() for the same object or do each of the threads have their own instances? In case, which is most likely the case, that all threads have their own objects, printB() methods will take much less time.
Since the methods are synchronized, the caller thread needs to acquire a monitor for that method. In case of the method being static, this monitor is class-specific, so essentially every thread tries to acquire a single monitor.
In case the method is non static like printB() each of the objects have their own monitors for the method. Since every thread is the only entity trying to acquire lock on object specific method-monitor in respective objects, these threads can work parallely.
Result, printB() takes much less time.
[Corrected, thanks to user fReaK]

- nosyDeveloper November 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

There exists only one copy of each method per class, be the method static or non-static. A thread needs to acquire the monitor of an appropriate object before entering a synchronized method which it releases when the thread returns from the method. In case of static synchronization the thread acquires the monitor of the class object, the locking is at class level so no other thread can execute it until the current thread releases the lock. In case of non-static synchronisation the thread acquires the monitor of that particular instance on which the call was made so other threads using different instances are not locked. They acquire monitors of there respective instances but continue to execute, as no two thread use the same instance. So what i feel is the non-static part will work faster i.e printB().

- fReaK November 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Clazz is heavier to lock. You can check the result with below code.
To put the mutex on it would take more time.

import java.util.Date;

public class Try {
public static void main(String[] args) throws InterruptedException {
Date d = new Date();
final Try2 t2 = new Try2();
for (int i = 0; i < 100000; i++) {
Thread t = new Thread() {
public void run() {
t2.test();
}
};
t.start();
t.join();
}
Date d2 = new Date();
System.out.println(d2.getTime() - d.getTime());


d = new Date();
for (int i = 0; i < 100000; i++) {
Thread t = new Thread() {
public void run() {
Try1.test();
}
};
t.start();
t.join();
}
d2 = new Date();

System.out.println(d2.getTime() - d.getTime());
}
}

class Try1 {
public static synchronized void test() {

}
}

class Try2 {
public synchronized void test() {

}
}

- Digi Digi November 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

printB() method takes less time because printA() is class label lock and printB() is object label lock ,in class label lock class allows only one thread at a time of different instances ,but in object label lock same instance allows only one synchronized method at a time .

- Anonymous November 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

printB() method takes less time because printA() is class label lock and printB() is object label lock ,in class label lock class allows only one thread at a time of different instances ,but in object label lock same instance allows only one synchronized method at a time

- Anonymous December 11, 2013 | 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