Also: have you, as part of your day job, ever written code to solve a specific word puzzle? (Based on an actual interview question I've answered.) An interview question is supposed to be representative; that doesn't mean every interview question must come directly from a specific problem encountered in production code. You ask questions to prompt people and see how they think and how they communicate.
1. it's a very simple thing to do, so why bother with a function? It's like asking someone to write a function that takes an integer value and returns an array of that length. 2. if you really do need a function that swaps numbers, then you probably also have some special edge cases that you will have to deal with that the candidate likely won't know about 3. each solution is language dependent
Maybe those aspects are why you consider it a good question, but when I have been asked those types of questions in the past I felt that the interviewer was missing an opportunity.
“$x * (5/25) + 10 * $y”
Of course it involved variable substitution, converting it to rpn, and then using the “Shunting Yard Algorithm”. I’ve had to do that twice. The second time I was maintaining a proprietary compiler/VM/IDE written in C++ for Windows CE. Yes, that was a dark time in my life.In fact, we were writing custom document creation software and outputting to industrial printers at the first job where we were doing a lot of text processing.