The answer to this question indicates that there are hierarchical differences in crew quarters on, for example, the Enterprise. Apparently, Lower Decks is considered canon, and so I seem to understand that the enlisted are not merely given less spacious quarters or forced to share rooms, but that they actually sleep in beds built into the walls of the main corridors.
Whilst it is of course unobjectionable that a starship would need a hierarchical command structure, it strikes me as odd that the radically egalitarian post-scarcity society of the Federation would decide to implement hierarchical perks in the matter of living space. That is, unless there were very serious practical requirements.
For that reason, I could understand if the Enterprise were a warship, and it was critically important for combat reasons that the hull size be kept to a minimum. Or if there were critical material shortages. These sorts of factors would make it reasonable that a captain or a commander or a lieutenant would get a larger room than the enlisted; it is better to have a larger room, as it improves comfort, allows for better mental recuperation, &c, and those benefits would be best allocated to the command staff first, for the greatest gain to overall efficiency, if there were a shortage.
But the enterpriseEnterprise is not a warship, and the Federation does not seem to be in any danger of running out of manufacturing capability, nor much concerned with keeping their ships to a tight profile. The Enterprise is basically a spaceborne city, complete with such amenities as gardens and schools, and the accepted answer to this question indicates that the Enterprise is probably up to 90% vacant on an average day. And the design of the ship in-totum, as well as the saucer segment specifically, doesn't seem especially concerned with the prospect of presenting a less easy-to-target profile.
So, why the unnecessary hierarchy? It seems tantamount to saying "the higher ranked are better than the lower, and they deserve better things (even when everyone could be equal)", which doesn't seem very much like the Federation to me.
Is there an in-universe reason for it? Does Starfleet think it "builds character" to be made to suffer simply for being lower in rank? Is it just that the writers found that mimicking the real navy/military was an easy way to go, and didn't think of the in-universe implications?