For which element 'X' is the carbocation $\ce{CH3-X-CH2+}$ the most stable?

A. X = S

B. X = N

C. X = O

I thought since oxygen is the most electronegative among the options, it would create the greatest electron deficiency, which would in turn facilitate a lone pair donation from the O atom (which happens with boron halides), so the answer should be oxygen. However the answer given is nitrogen. I really can't understand why. Is my logic wrong? Or is there more to this question?

A carbocation is electron deficient. In an electron-neutral state, carbon would have four valence electrons that form four bonds resulting in an overall octet structure. To form a carbocation, one of these bonds needs to be removed, resulting in a sextet on the carbon atom; and one could also consider carbon now only having three valence electrons that form three bonds. Whichever picture we choose, carbon is lacking electrons.

(A more true picture would, of course, involve orbitals and their interactions, but still leave us with the conclusion that there are too few electons around the carbon atom.)

The three possible neighbours differ, amoung others, in their electronegativity — their ability to draw electrons towards them, away from what is next to them. Oxygen is the strongest, nitrogen second strongest and sulphur is the weakest.

In the case of the carbocation, I have already shown that it is electron-deficient. If we place a more electronegative element next to it, that element will draw the electrons away from the cation even more; this makes the carbon even more electron-deficient and thus destabilises the system. On the other hand, if a more electropositive element such as silicon were neighbouring the carbon, it would push electrons towards the carbon — exactly what the cation wants since it’s lacking electrons.

By this reasoning, sulphur, being the least electronegative element on the list should best stabilise the carbocation in question. But the answer gives nitrogen; so there must be a second effect. This would be mesomeric stabilisation.

For all three elements, one can draw a resonance structure where instead of a single $\ce{C-X}$ bond and a lone pair on $\ce{X}$ one has a $\ce{C=X}$ double bond shifting the positive charge to $\ce{X}$. This resonance structure is considerably less prevalent with sulphur (a third period element) than with oxygen or nitrogen; the reason being the spacial distribution of sulphur’s p-orbitals not fitting too well with carbon’s. Oxygen and nitrogen, on the other hand, have p-orbitals of a similar size and distribution so mixing works well. (Yes, this is also more complex on a more realistic level, but this visualisation works well enough for the simple case here.)

The question is now, whether the resonance stability provided by oxygen is stronger or weaker than that of nitrogen and if the difference is larger or smaller than the energy difference caused by the different electronegativities discussed earlier.

Again arguing that oxygen is more electronegative and thus draws electrons towards it, it would equally be less happy to share its unbonded electron pair with carbon to relieve carbon’s electron deficiency. Thus, one may expect nitrogen to provide a greater stabilisation to a neighbouring carbocation.