Google Interview Question

Country: United States
Interview Type: Phone Interview

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

Create a map<Thread, List<Locks>>.

1) Every time a thread request a lock, it will store the data of itself and the lock in the map
2) Thread A requests a lock 5, it locks the object, and does map.put(ThreadA, List.add(lock5));
3) Thread B requests a lock 3, it locks the object, and does map.put(ThreadB, List.add(lock3));
4) Thread B requests a lock 5, the lock is held
5) Thread A requests a lock 3, the lock is held (deadlock occurred)
Detection: Thread A traverses through Map and sees that Thread B is holding lock 3 and waiting on lock 5 (deadlock detected)

Then we need to execute any deadlock prevention mechanisms like releasing all locks, backing up, and redo request at random time

- Dan April 26, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
of 2 vote

In Java 6 and beyond use:

ThreadMXBean mbean = ManagementFactory.getThreadMXBean();
mbean.findDeadlockedThreads(); // returns the Ids of threads deadlocked

- guilhebl April 18, 2018 | Flag Reply

Add a Comment

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.


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


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