Solution:
Concept: A metal of valency 2 loses two electrons; a non-metal of valency 3 gains three. In the compound the electrons lost must equal the electrons gained.
Step 1 — (a) Form the ions.
X loses two electrons and becomes a cation, while Y gains three electrons and becomes an anion:
X−2e−⟶X2+
Y+3e−⟶Y3−
Step 2 — (b) Find the formula.
Three atoms of X give 3 × 2 = 6 electrons, and two atoms of Y take 2 × 3 = 6 electrons, so the compound is X3Y2 (like Mg3N2). Since Y is diatomic, one Y2 molecule supplies the two Y atoms:
3X+Y2⟶X3Y2

Step 3 — (c) Electrolysis of the melt.
In the melt the X2+ cations move to the cathode and gain electrons, while the Y3− anions move to the anode and lose electrons:
X2++2e−⟶X
Y3−−3e−⟶Y
Watch out: The formula takes the LOWEST numbers that balance the electrons: 3 X and 2 Y (6 electrons each).
Therefore, (a) X − 2e⁻ → X²⁺ and Y + 3e⁻ → Y³⁻; (b) 3X + Y₂ → X₃Y₂; (c) X is obtained at the cathode and Y at the anode.