All perfect praise be to Allaah, The Lord of the Worlds. I testify that there is none worthy of worship except Allaah, and that Muhammad is His slave and Messenger.
It is permissible for you to buy the house that you mentioned in the question and there is no impediment that prevents from buying a house that is adjacent to the mosque or that is beside it. Rather, it might be that this is better in order to observe the prayer in the mosque and maintain it through performing acts of obedience to Allaah.
Allaah Knows best.