AI writes code that calls a library function you have never seen. How do you check that it really exists?
Suggested answer
The interviewer wants to know whether you treat AI-written code as a draft that needs verifying, and whether you know cheap, fast and reliable ways to verify it, rather than just asking the AI again.
The most reliable way is to use a source the AI cannot make up. You look for the function name in the code or the type files of the library you have installed, for example by searching inside node_modules. If you can't find it, the function doesn't exist in the version you use. Asking the AI "does this function exist?" is a weak method, because it can answer confidently in exactly the same way it invented the function at first.
There are three layers of verification, from cheap to thorough:
- Run it. This catches errors on the path the program takes, but you only see errors one at a time, and an error in a branch that rarely runs will sleep until a real user hits it.
- The compiler or a linter. With TypeScript,
tsc --noEmitreads the whole file and reports every call with the wrong shape in a single pass. One error at the top of a chain can cause follow-on errors, so fix the first one and run it again. - Tests. These catch behavior that you thought of checking, for example "editing this person's message doesn't touch the other person's ledger".
An important point to make: none of the three layers catches code that has the right shape but the wrong meaning. A function that does exist but is used wrongly, or one that searches shared data instead of each person's own data, passes all of them. So in the end you need to read the official documentation for things like "within what scope is this ID unique", and write tests yourself for the situations that matter.
Bonus points: say that you treat every unfamiliar function the AI offers as a question to answer, not as an answer, and mention that made-up function names are often borrowed from a similar library (like a function from Telegraf showing up in grammY).