Join tables to map userId to name
Company: Karat
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
## Problem
You are given three tables represented as **2D string arrays** (each row is a record, no headers). You need to produce a mapping from **`userId` → `customerName`** by matching and joining information across tables.
### Table 1: `customers`
Each row: `[customerName, email, productName, quantity]`
- `quantity` is an integer in string form.
- Assumption: **each customer buys only one type of product** (i.e., one `productName` per customer).
### Table 2: `orders`
Each row: `[userId, email, orderTotal]`
- `orderTotal` is a number in string form (integer or decimal).
- `email` **may be null** (or an empty string, depending on how the input is represented).
### Table 3: `products`
Each row: `[productName, unitPrice]`
- `unitPrice` is a number in string form.
## Matching / Join requirement
For each `order` record, identify which customer placed it, then output `userId -> customerName`.
Use these matching rules:
1. If `orders.email` is present (non-null/non-empty), match the customer by **email**.
2. If `orders.email` is missing, match by using the fact that:
- `orderTotal = quantity * unitPrice` for the customer’s purchased product.
- Use `customers.productName` joined to `products.productName` to get `unitPrice`.
## Output
Return a mapping/dictionary from `userId` to `customerName` for all orders.
## Assumptions / Edge cases
- If multiple customers could match an order (e.g., same computed total), specify and implement a deterministic tie-breaker (e.g., pick lexicographically smallest name) or return all candidates—clarify with the interviewer.
- Assume inputs are well-formed aside from possible null/empty emails.
Quick Answer: This question evaluates a candidate's skills in relational data joining, data integration, and numeric reconciliation for mapping userId to customerName across multiple tables.