As an interviewer, I face a number of challenges. If I just ask you about coding, I can rule out people who really don't know what they're talking about, but I might accidentally hire someone who "talks a good game" but writes horrible code - hard to read, insecure, bug-ridden, inefficient - or who is very slow, or who is rude to co-workers, or much more. So I ask you questions that aren't just about code, in the hopes I can learn enough about you to avoid a bad hire, but that isn't always enough either.
Some companies set coding problems in the interview, either on a whiteboard or at a computer. These are really expensive for the company because they add 30 or 60 minutes to the interview in many cases. A good programmer might do a bad job at these (so we recommend practicing for them) and a bad interviewer might misinterpret what happens during them.
As a result some companies think they will save money and time by giving you a sort of "take home test". They don't typically ask you to write something they want to sell, it's a way to see what you can do. Often it's a problem that was solved already so they can compare your solution to someone else's. There's still the risk that someone might help you do it, or you might spend far longer on it than you say, but they see it as a cheaper way to screen people. If you want the job, you'll invest the time in being screened, and if it's not worth it to do that, just decline the opportunity. This happens in other industries: cooks are sometimes asked to work unpaid in the kitchen for a day so their speed and technique can be evaluated, artists are asked to bring a portfolio which often includes works they created without being paid to do so, performers must audition - give an unpaid performance - and often spend time learning music, lines, or steps in order to do so, and so on.
I have heard people suggest that somehow they are not so much interviewing as doing free project work. That companies are asking for a day or two of work, then when it's submitted asking for another day or two of work, all while dangling a job offer in front of them. If this is happening to you, one of two situations exist:
- you need a day or two for something that the ideal candidate would need only an hour to do, and they are screening hard to make sure they get that candidate
- you are misunderstanding what they ask of you and doing much more than they wanted, so that either you or they have huge communication shortfalls
- they are exploiting job applicants to get their web site built or some other small task done cheaply
The good news is, it doesn't matter which of these are true - you just don't want to continue applying for this job. You don't want to work for the place whose web site was glued together from samples provided by applicants of varying quality, or who doesn't mind exploiting vulnerable or desperate people, you don't want to work with a company that cannot explain its needs to you, and you won't get to work at the place where you need to be much faster or better than you are now. So thank them and decline to go further into the process.