Microsoft Interview Question
Software Engineer / DevelopersRight! They can initially exchange the keys using the above approach and then sends messages.
I dont think , its the right way......
The first time they will not exchange any mesage .
The A will simply lock one latch with the lock . leave the box open.
Now when this will reach to B. B will lock the 2nd latch with the A's lock.
Left letch ----A lock --- B Lock ------right latch.
Now this system will alow any of A or B to communicate with theier own key.
If any of the lock is open ........box will be open.
A puts the message in lockA and locks it, and put key A to lockB and locks it. when B receives the box, he first uses his KeyB to open lockB to get keyA, then use keyA to open lockA to get the message. the assumption is that the latch can be locked without using keys.
actually no..! the assumption is that as long as theres at least one lock on the box, the box cannot be opened. Its a little hard to imagine such a physical box.
Using this PP's solution above makes sense. At somepoint the the box is double locked, after which each person can open a lock. The second person to open the lock also gets the message.
Another solution is,
If A wants to deliver message to B frequently, he should let B send a lock B to him. Then he will use lock B to lock his message and lock A in the box. B will use key B to open lock B. Then B can put his message and lock B inside the box and lock the box using lock A. So A could use key A to open the box, get the message from B and get lock B....
The postman can not open the box at anytime since the key is never being exchanged. A always use key A to open the box and B always use key B to open the box. Only if they exchange the lock at the first round.
Any comments for this?
1. A puts MesgA and KeyA into box and locks with LockA.
2. A sends box to B.
3. B receives it. Locks it with LockB. Box is now locked with both LockA and LockB.
4. B sends box to A.
5. A unlocks LockA. Box is now locked with both LockB.
6. A sends box to B.
7. B unlocks LockB. B recieves MesgA and KeyA.
Moving On B has A's key. B can share KeyB is further communication with A.
. A puts MesgA and KeyA into box and locks with LockA.
2. A sends box to B.
3. B receives it. Locks it with LockB. Box is now locked with both LockA and LockB.
4. B sends box to A.
5. A unlocks LockA. Box is now locked with LockB.
6. A sends box to B.
7. B unlocks LockB. B recieves MesgA and KeyA.
Moving On B has A's key. B can share KeyB is further communication with A.
How about this?
- PP June 14, 2007Suppose A has to send a message to B, then A will lock the box with L(A) the "lock of A" and send it to B. B can't open it B will lock the box with L(B) and send it to. A will unlock L(A) and send it to B and then B can unlock L(B) and get the message.