From the shiurim of Rabbi Elchanan Peretz shlita on monetary law, damages and the laws of robbery
The apartment belongs entirely to the first buyer, and the transaction made with the second buyer is void from its inception.
The Shulchan Aruch rules (חושן משפט, סימן שע"ג סעיף א'): "הגוזל שדה או המוכר שדה שאינה שלו — אין מעשיו קיימים כלום". Once the first buyer signed the contract and performed a kinyan properly and in accordance with halacha, the apartment left the contractor’s possession and passed into the ownership of the first buyer. That being so, the contractor had no legal or halachic power to sell this apartment to the second buyer. The first buyer is entitled to demand delivery of the specific apartment he bought, and he is not obligated to agree to accept an alternative apartment in the building (Shulchan Aruch, Choshen Mishpat 237:1).
The answers were compiled from the shiurim of Rabbi Elchanan Peretz shlita and from rulings he received from HaGaon Rabbi Chaim Pinchas Scheinberg zt”l and from leading poskim, and are for study purposes only. One may not rely on what is stated here for practical halachic ruling; for a personal question, one should consult a rav or a Beit Horaah.