Speaking as an employer: we address this by keeping our coding exercise short, and specifying a time limit. The limit ("60-90 minutes") is not rigidly enforced, but candidates know we're able to see how long they spent, and in practice almost everyone spends more than 60 and less than 90 minutes.
It's tricky. An in-person test would put the candidate in an unfamiliar environment, and makes many people nervous. A take-home test without a time limit opens itself up to the "I'd better spend lots of extra time so I can look impressive" problem. A take-home test with a hard time limit can also make people nervous. This is the best compromise we've been able to come up with (suggestions for improvement welcomed!).
As suggested on this thread, we also don't ask candidates to tackle the exercise until they've had a chance to talk to us on the phone.
It's tricky. An in-person test would put the candidate in an unfamiliar environment, and makes many people nervous. A take-home test without a time limit opens itself up to the "I'd better spend lots of extra time so I can look impressive" problem. A take-home test with a hard time limit can also make people nervous. This is the best compromise we've been able to come up with (suggestions for improvement welcomed!).
As suggested on this thread, we also don't ask candidates to tackle the exercise until they've had a chance to talk to us on the phone.