VALLEJO, Calif.—Soaring home prices and apartment rents in Silicon Valley have become a growing headache for technology titans. Google owner Alphabet Inc. is taking a step toward addressing the issue.

The Mountain View, Calif., company is finalizing an order to buy 300 apartment units from Factory OS, a modular-home startup, in a building likely to serve as short-term housing for Google employees, according to executives from both companies.

The...