Skip to main content

You have an orders table and a customers table that share customer_id. Join them and list the orders that have no matching customer. For example, if order 3 has customer_id 99 and no customer has that id, order 3 should be in the result.

Coding rounds · Intermediate 1

Short answer

Do a left join from orders with indicator=True, then keep rows where _merge is left_only: orders.merge(customers, on="customer_id", how="left", indicator=True). A merge is a hash join, about O(n + m). Check that customer_id is unique in customers first, or the join will multiply rows.

Deeper answer

First clarify the keys. Ask whether customer_id is unique in customers, whether the key columns have the same type in both tables, and whether orders can have a missing customer_id.

The steps are: merged = orders.merge(customers, on="customer_id", how="left", indicator=True, validate="many_to_one"). The indicator adds a _merge column with the values both, left_only and right_only. Then unmatched = merged[merged["_merge"] == "left_only"]. The validate argument raises a MergeError when customers has duplicate keys, so the row count cannot grow without you noticing. Compare len(merged) with len(orders) as a second check. If you only need the orders, an anti-join is shorter: orders[~orders["customer_id"].isin(customers["customer_id"])].

The cost is roughly O(n + m) time, plus memory for the merged table. Test an order with an unknown id, an order with a missing id, and keys stored as text in one table and integers in the other, which pandas refuses to merge. Say that how="outer" also shows customers with no orders as right_only.

What a strong answer covers

Answer it out loud in the mock interview about 2.5 min

Follow-ups to expect

Common mistakes

Learn it

Tables with pandas

Practice Coding rounds questions

More on this topic

Today's AI turns are used

Lessons, roadmaps, videos, practice cards and the code playground stay free with your account, with no limit.