Q. When two individuals meet, one exiting a shul or a beis hamedresh and the other about to enter, who has din kadima and goes first?
A. Rema Mipano (Teshuvos 83) rules in regards to two individuals, one who is given an aliya to read the Torah and the other now returning to his seat after finishing his aliya, that the one being called goes first. He explains that the one who has already read first, has already been honoured and now it is his turn to honor the one called next. He proves his point from Talmud (Suka 56a) that the incoming Cohanim enter the temple court from the preferred right (the north) while the exiting leave from the left (south). He goes even further and mentions that the above would apply even when honouring a disciple before his master.Ginas Verodim (O.H. 1: 23) rules similarly and states that the person about to comply with a mitzva preempts the one who has already done so. However, he maintains the this only applies when both are of equal stature and the talmid should always honor his Rebbi. (see also Torah Lishma 59).
Horav Shlomo Miller's Shlit'a opinion is that when we are not dealing with people about to do a mitzva but rather when they are engaged in reshus or optional activities, and both individuals are of equal status, conventional wisdom and commonly accepted protocol dictates that the one exiting goes first, in order to provide space to the incoming one.
Rabbi A. Bartfeld as revised by Horav Shlomo Miller Shlit'a