Walmart Labs Interview Question
Software DevelopersTeam: Customer experience
Country: United States
Interview Type: In-Person
This is similar to typical producer consumer problem except a small difference. We don't need to block writing while reading and vice-versa. Here is pseudo code:-
mutex m1
mutex m2
semaphore sem_full = SIZE_Q
semaphore sem_empty = 0
Read()
{
wait(sem_empty)
m1.lock()
//Do read operation of first element from Q
m1.unlock()
post(sem_full)
}
Write()
{
wait(sem_full)
m2.lock()
// Do write operation to write at back
m2.unlcok()
post(sem_empty)
}
initializations_
Assume that the queue has functions named remove() and insert(). Also, we have a function named generate() to create data to be entered in the queue
reader_code
Writer code is similar
writer_code
Please let me know if there is a starvation or deadlock.
- footloose June 26, 2015