Atomic number of sodium - 11
Electronic configuration of sodium - 1s22s22p63s1
It has one valence electron, it will lose one electron to get octet configuration
Atomic number of oxygen - 8
Electronic configuration of oxygen - 1s22s22p4
It is two electrons short of octet configuration, thus it will gain two electrons.
During the bond formation between sodium and oxygen, two sodium atoms loses one electron each to oxygen atom,
2Na−2e−→2Na+O+2e−→O−22Na++O−2→Na2O
Atomic number of Magnesium - 12
Electronic configuration of Magnesium -1s22s22p63s2
It has two valence electrons , it will lose two electrons to get octet configuration
Atomic number of oxygen -8
Electronic configuration of oxygen −1s22s22p4
It is two electrons short octet configuration, thus it will gain two electrons.
During the bond formation between Magnesium and Oxygen, one magnesium atom loses two electrons to oxygen atom.
Mg−2e−→Mg2+O+2e−→O−2Mg+2+O−2→MgO