Sharing an interview from a less commonly discussed company.
The problem was to have multiple threads print the numbers from 0 through n in alternating turns.
Given a number n, three specified threads must cooperate to print every number from 0 through n in order. The output must also identify which thread printed each number.
The thread responsibilities were:
ZeroThreadprints 0, and 0 can be printed only once.OddThreadprints all odd numbers: 1, 3, 5, 7, and so on.EvenThreadprints all even numbers: 2, 4, 6, 8, and so on.
The required output format was:
0 printed by ZeroThread
1 printed by OddThread
2 printed by EvenThread
3 printed by OddThread
4 printed by EvenThread
...
n printed by respective thread
The design constraints were:
- Implement three independent thread classes:
ZeroThread,OddThread, andEvenThread. - Keep the printing logic inside the corresponding thread class. For example, the logic for printing 0 belongs in
ZeroThread, the odd-number logic belongs inOddThread, and the even-number logic belongs inEvenThread. - The printed numbers must remain in strictly increasing order.
Discussion
Loading comments…