I was invited to an unexpected and urgent second postdoc interview (in less than 24h) - Is this fair?
It's not respectful to applicants for the interview process to be unclear or changed at the last minute. However, for various reasons, many of them completely innocent (busy people, miscommunications), this may happen.
It's also not always transparent what an interviewer is looking for. If the interview has you attempt &quot;Task A&quot;, it may not be performance on Task A that you are being evaluated on. Instead, you may be evaluated for how you handle an unexpected change in plans. Or you may be evaluated on how you ask for clarification around a task, how you manage time if a task cannot be completed within the window you are given, what you do when you find you are stuck on something (do you ask for help? Stop and admit you are stuck? Make something up that might work? Pretend you know what you're doing?), etc.
Judging &quot;success&quot; on this sort of task is entirely up to the interviewer's views and opinion. As far as whether it's &quot;fair&quot;, well...the definition of &quot;fair&quot; is a moving target and various opinions would give various descriptions of &quot;fair&quot;. In one sense, as long as the same information is communicated in the same time frame to all the candidates, and all candidates are judged by the same criteria, it is &quot;fair&quot; among those candidates.
As far as whether it's the process that selects the true best candidate for the job, well... if we had a better way of determining the best candidate than the clunky interview process, we'd use that method instead.

I'm not sure you can do much about this except for continuing to interview for positions that interest you. Positions at desirable universities are likely to attract many qualified applicants, and you aren't guaranteed a job just because you're qualified. Ultimately, they have to select someone, and it's not even really possible for the person doing the hiring to know that they've got the best one.
I'd recommend not spending more time thinking about the process of this one interview. Maybe their expectation is that programming is a bigger part of the role but didn't think it was necessary to mention it. Maybe they had a preferred candidate already but lacked the justification they needed and invented this task on the fly to create it. Maybe they're disorganized. The answer, whatever it is, won't help for your next interview. Maybe you even dodged a bullet and this is a bad work environment, and had you been hired your next Academia.SE question would be about being stuck in a post doc where you're just treated as a programmer and aren't making any progress on independent research projects. Move on and forget this one.