APIs That Do Not Exist

Topics Covered

Why Assistants Invent APIs

Where invented calls come from

Why they are dangerous in a round

What invented calls look like

A first example in Python

A first example in TypeScript

A first example in Java

The habit this lesson builds

A drill for spotting invented calls

Invented calls and the twist

Invented calls in tests

Invented calls on project code

How the interviewer sees it

One of the most distinctive mistakes in assistant code is a call to a function, method or option that does not exist. The code reads well, the name is plausible and the surrounding logic is often correct, but the moment it runs it fails with an attribute error, a reference error or a compile error. Sometimes it is subtler: the function exists, but not with those arguments, or not in this version of the library, or it does something slightly different from what the code assumes.

This lesson covers how to spot these calls in review, how to check them in seconds and how to fix them without rewriting the code around them.

Invented methods are assembled from familiar parts, which is why they look real.

Where invented calls come from

An assistant writes code by predicting what is likely to come next, based on an enormous amount of code it has seen. When it reaches a point where it needs to, say, sort a dictionary by value, it predicts a call that looks like the calls it has seen in similar places. Usually that prediction is a real method. Sometimes it is a blend: a real object combined with a method name that exists on a different object, in a different library or in a different language. The result has every surface feature of a real call.

Invented calls are more likely in some situations than others. They are common when the task needs something slightly unusual, such as a sorted view of a dictionary, a grouped count or a specific date format. They are common in less popular libraries, where the assistant has seen fewer examples. They are common when a library has changed between versions, so the assistant mixes old and new names. And they are common when the same idea is spelled differently in different languages, so a habit from one leaks into another.

Why they are dangerous in a round

In a real project, an invented call usually fails fast: the code does not run, someone notices, it gets fixed. In an interview round, the cost is different. The call may be on a code path the visible tests do not exercise, so the tests pass and the candidate moves on. Or the candidate sees the error but assumes it is something else, an import problem or an environment issue, and spends minutes on the wrong fix. Or the candidate asks the assistant to fix the error, and it replaces one invented call with another.

There is also a signal cost. An interviewer watching a candidate accept a call to a method that does not exist sees exactly the behaviour the round is designed to detect: trusting output without checking it. Catching an invented call before running the code, on the other hand, is one of the most visible ways to show that you review rather than accept.

What invented calls look like

Invented calls tend to have a few tells. The method name is a perfectly reasonable English description of what the code needs: sort by value, flatten deep, group by key, to snake case. The object it is called on is a basic built in type, such as a dictionary, a list, a string or an array, that in fact has a small, fixed set of methods. The call is often the single line doing the hardest part of the job, while the lines around it are routine. And it frequently comes with a confident comment explaining what it returns.

None of these tells proves a call is invented. Plenty of real methods have reasonable English names. But when several of them line up, the call deserves a quick check before you read anything else.

A first example in Python

diff
1 def top_customers(orders, n):
2     totals = {}
3     for customer, amount in orders:
4         totals[customer] = totals.get(customer, 0) + amount
5-    ranked = sorted(totals.items(), key=lambda kv: -kv[1])
6-    return [c for c, _ in ranked[:n]]
7+    # sort_by_value returns (key, value) pairs, highest value first.
8+    return [c for c, _ in totals.sort_by_value(reverse=True)[:n]]

The assistant was asked to make the function shorter. It replaced a correct sort with a call to sort by value on a plain dictionary. Python dictionaries have no such method. The comment describes exactly what the code needs, which makes the line easy to accept. Running the function raises an attribute error on the first call.

A first example in TypeScript

diff
1 function uniqueTags(posts: Post[]): string[] {
2-  return [...new Set(posts.flatMap(p => p.tags))].sort();
3+  return posts.flatMap(p => p.tags).unique().sort();
4 }

Arrays in JavaScript and TypeScript have no unique method. Several popular utility libraries provide one, and some other languages have one built in, so the name is familiar. A type checker would flag it immediately as a property that does not exist on the array type, which is one reason to keep type checking on in TypeScript rounds.

A first example in Java

diff
1 static int total(List<Integer> amounts) {
2-    int sum = 0;
3-    for (int a : amounts) sum += a;
4-    return sum;
5+    return amounts.sum();
6 }

Java's List interface has no sum method. The idiomatic equivalent uses a stream, mapping to int and then summing. The invented version will not compile, which in Java at least makes it fail immediately.

The habit this lesson builds

The habit is simple: for every call in a diff that you do not personally know exists, with those arguments, in this language and version, check it before you review the logic around it. The rest of the lesson shows how to recognise such calls, how to check them quickly and how to replace them cleanly.

A drill for spotting invented calls

Ask an assistant, in a fresh session, to write short functions for ten slightly unusual tasks in each of the three course languages: sort a map by value, group items by a key, take the last n items, title case a string, find the most common element, chunk a list, deep merge two objects, compute a running total, parse a date in a given format and format a number with thousands separators. Before running each, mark every call you are not certain exists. Then run them. Keep a list of the invented calls you find; it will be surprisingly similar from session to session, which is exactly why it is worth knowing.

Invented calls and the twist

Invented calls are especially common right after the twist. The twist usually asks for something the existing code does not do, such as a new ordering, a new grouping or a new time calculation, and the assistant reaches for a helper that would make the change one line. Under time pressure, that one line is very tempting to accept. Treat any new library call introduced in response to the twist as a suspect until checked; it takes seconds, and the twist is the worst moment to lose minutes to an attribute error.

Invented calls in tests

Tests are not immune. An assistant writing tests may call an assertion helper that the test framework does not provide, such as an assertion for unordered list equality or for approximate dictionaries. Some frameworks have these and some do not. When a test file gains a new assertion style, check it exists in the framework the project uses. A test that fails because of an invented assertion can look like a failing feature, which sends you debugging the wrong thing.

Invented calls on project code

Assistants can also invent methods on the project's own classes: a store dot find by user method when the store only has get and list, or a service dot validate method that was never written. These fail just as loudly but are harder to spot from memory, because you know the project's classes less well than the standard library. During orientation, note the main public methods of the core classes. When a diff calls something on a project class that is not on your list, search for its definition before reading further. If it does not exist, either the assistant expects you to write it, which it should have said, or it invented it.

How the interviewer sees it

Interviewers often include code paths where an assistant is likely to reach for a plausible but missing helper, because it is a clean test of whether the candidate checks output. When you catch one, say so plainly and move on: that method does not exist on dict; replacing it with sorted. There is no need to dwell on it. The quick, calm catch is the signal.

A plausible name is not evidence

Invented methods are built from familiar parts: a basic type plus a sensible verb. The more naturally the name describes what the code needs, the more carefully you should check that it exists.