(a) Magnesium loses two electrons to attain a noble gas configuration, whereas oxygen gains two electrons to completely fill the valence shell.
Mg → Mg2+ + 2e–
O + 2e– 1→ O2–
(b) Sodium loses one electron to have a completely filled valence shell, whereas oxygen gains two electrons to complete the valence shell.
Na → Na+ + e–
O + 2e– → O2–