It's illegal in the United States to discriminate on a basis that has nothing to do with the job. So if your job doesn't require people with sharp analytical skills (trucking, for example), it would be illegal to use an IQ test. In programming, I doubt it would be illegal.
The reason for this is that people who wanted to exclude black people or women from their workforce would pick some irrelevant trait that the disadvantaged group would score lower on, and use that as the reason not to hire.
Some of these puzzle/problems are stupid, but if you do not know how to solve problems well and frequently you probably will not be a good (let alone excellent) software developer.
These puzzles aren't necessarily used in interviews, and I'm sure that employers who are looking for intelligent employees have several ways of attaining them.
If so, then I think these puzzles are just a way to get around this law.
The job may not involve much puzzle-like thinking, but it never hurts to have high IQ employees.