Amazon Interview Question for SDE1s


Country: United States




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

This question is based on thread scheduling. Normally, if you were designing a thread scheduler and had access to the internals, you would change the priority of each thread such that Thread 1 would have the highest priority, Thread 2 would have a lower priority and Thread 3 would have the lowest priority. And if the scheduler dispatched the threads according to the priorities, then T1, T2, T3 would be executed in that order.
However, in most systems, since you can only treat the Thread Scheduler as a black box, you can only use the join() method to control the execution of the threads. One simple way would be to have thread1 call join() and when it finishes, thread2 calls join() and so forth. In essence, calling join() will make the other thread wait. I have implemented my solution in Python below.

My solution:

import threading
import time

def worker():
    print(threading.currentThread().getName(), 'Starting')
    time.sleep(2)
    print(threading.currentThread().getName(), 'Exiting')

t1 = threading.Thread(name='Thread 1', target=worker)
t2 = threading.Thread(name='Thread 2', target=worker)
t3 = threading.Thread(name='Thread 3', target=worker)

t1.start()
t1.join()
t2.start()
t2.join()
t3.start()
t3.join()

- prudent_programmer March 05, 2018 | 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