Solution:
Concept: The valency of an atom is the number of electrons it loses, gains or shares to reach a noble gas configuration. Atoms with 1, 2 or 3 valence electrons lose them and form positive ions; atoms with 5, 6 or 7 valence electrons gain 3, 2 or 1 electrons and form negative ions. An electric current passes through a liquid or a solution only if it contains ions that are free to move. Positive ions (cations) move to the cathode (negative electrode) and negative ions (anions) move to the anode (positive electrode).
Step 1 — Identify A and B.
The M shell is the third shell, so A is 2, 8, 2 (magnesium): it loses 2 electrons. B (atomic number 7) is 2, 5 (nitrogen): it gains 3 electrons.
Step 2 — Formation of the ions.
(a) A loses its two valence electrons and B gains three electrons.
A→A2++2e−
B+3e−→B3−
Step 3 — Direct combination.
(b) To balance the charges, 3 A²⁺ (6+) combine with 2 B³⁻ (6−), so the compound is A₃B₂. With B as the diatomic gas B₂, the equation is:
3A+B2→A3B2
For magnesium and nitrogen this is magnesium nitride:
3Mg+N2→Mg3N2
Step 4 — Electrolysis of the molten compound.
(c) In the molten compound the ions are free. The cation A²⁺ moves to the negative electrode, so A is obtained at the cathode; the anion B³⁻ moves to the positive electrode, so B is obtained at the anode.

Watch out: The charges must balance: 3 × (+2) = +6 and 2 × (−3) = −6, which is why the formula is A₃B₂.
Therefore, (a) A → A²⁺ + 2e⁻ and B + 3e⁻ → B³⁻; (b) 3A + B₂ → A₃B₂; (c) A is obtained at the cathode and B at the anode.