Very interesting problem! The supplied solution is simple and it works for that specific use case, but there are still some problems.

1) This can only be done where its feasible to connect the two doors with a rope. What if the doors are physically seperated far away from each other, or maybe even seperate entrances to a large bulding complex?

2) You can be locked inside the bathroom if someone locks the door behind you.

So i tried coming up with my own solution to solving the specified problem while also dealing with the problems with the other solution.

First off it makes sense having a system with keys and devices that are easily installed at each door. That way you deal with problem 1.

With this solution another problem arises: You can just remove a key from the room and nothing will work, so a constraint is introduced.

Specifically:
It should be impossible to exit the room while carrying any of the keys, given you don’t work together with the opposite side.

So my solution is:
A connected sliding lock on both sides of both doors, plus a sliding lock containing a key on both doors inside the bathroom. So one sliding lock in the apartment side and two locks the bathroom side for each door. Inside the bathroom you can only lock the opposite sides door by locking your own door first. Locking your own door with the sliding lock containing a ley gives access to the key which you insert into one of the sliding locks on the opposite side. An important feature is that you can not remove the key from the lock on the opposite side without first unlocking it and you can not unlock your own door without using the key.

So with this system it is impossible to leave with a key from the bathroom, and you also can’t lock your opponents door without locking your own door, barring your escape.

At first I thought this solution was the perfect one, but then I realised that there is nothing stopping someone from stealing the key from the opposite side by just locking the opponent door with their sliding lock, removing the key, and leaving the room.

The solution to this is that the sliding lock for eachs side key is connected to the sliding lock on the apartment side. By this I mean that when the door is locked on the apartment side, it will make the sliding lock on the bathroom side unmovable, and aldo vice versa. If you have locked the door on your side, the sliding lock will also be immovable. This way nobody can lock you inside, and you can not steal the opponents key because it is locked from its apartment side therefore blocking the use of the sliding lock with the key on the bathroom side.

So I think this solution works, even though it is somewhat complicated. So heres the user story for using the bathroom.

1. Unlock door on apartment side.
2. Lock door on bathroom side, and take key.
3. Use key to lock door on opposite side
4. Do your business
5. Unlock opposite side, and take key
6. Use key to unlock your own door, locking the key in place in he process
7. Exit the bathroom and slide the lock again.

I guess this is what you would call «over-engineering» but it was fun trying to come up with a solution!

Please tell me if I’ve missed something that would break this, I can’t come up with anything atm.