Can I build a masjid on behalf of someone who’s passed away?

Yes, you can give Sadaqah Jariyah on behalf of the deceased.