Write SQL and merge linked lists
GoogleData Engineer · Technical ScreenLast updated
Solve a SQL customer order aggregation task and merge two sorted linked lists. The solution uses CTEs, window functions, LEFT JOINs, COALESCE, latest-order tie handling, optional returned-order filtering, and an O(m+n) dummy-head linked-list merge.
The technical interview included two coding-style tasks: a SQL analytics query and merging two sorted linked lists.
Constraints & Assumptions
- For SQL, return one row per customer, including customers with no orders.
- Report
total_orders,total_amount,latest_order_date, andlatest_order_amount. - Use a nested query, CTE, or window function to identify each customer's most recent order.
- For linked lists, reuse existing nodes and return the merged head.
Clarifying Questions to Ask
- If a customer has multiple orders on the same latest date, how should ties be handled?
- Should returned orders affect total order amount?
- Should
total_amountbe0orNULLfor customers with no orders? - Are linked-list values sorted in non-decreasing order?
Part 1 - Write The SQL Query
Given customers, orders, and returns, write a query returning customer-level order metrics and latest order amount.
What This Part Should Cover
- Aggregate orders by customer.
- Use
ROW_NUMBER()or similar to find the latest order per customer. LEFT JOINfrom customers so customers without orders remain.COALESCEfor zero counts and amounts if desired.
Part 2 - Merge Two Sorted Linked Lists
Given heads of two sorted singly linked lists, merge them into one sorted list by reusing nodes.
What This Part Should Cover
- Dummy head and tail pointer.
- Compare current node values and append the smaller node.
- Attach the remaining suffix.
- Complexity and edge cases.
Follow-up questions
- How would you exclude returned orders?
- How would you handle ties in latest order date?
- How would you merge `k` sorted linked lists?
- What is the recursive linked-list solution?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.