Day 1 — Two Sum

Coding problem

ProblemTwo Sum
LeetCode ID(s)LeetCode #1
DifficultyEasy
PatternHash Map
Company tagsAll Companies
Suggested time10m

Solution outline (coding)

Focus on the Hash Map pattern. Start by writing out a few examples by hand, then identify the invariant you must maintain (e.g., prefix sums, window bounds, visited set, heap ordering). Aim for an implementation you can explain in under a minute, including time and space complexity.

Show Python solution
from typing import List


class Solution:
  def twoSum(self, nums: List[int], target: int) -> List[int]:
    # Hash map from value -> index seen so far.
    # For each x at index i, we look for (target - x) among earlier elements.
    seen = {}

    for i, x in enumerate(nums):
      want = target - x
      if want in seen:
        return [seen[want], i]
      seen[x] = i

    # LeetCode guarantees exactly one solution.
    raise ValueError("No valid pair found")


# Input:  nums = [2, 7, 11, 15] , target = 9
# Output: indices = [0, 1]

SQL question

transactions(user_id, ts, amount), dim_users(user_id, country). BigQuery: top 3 countries by total amount in last 30 days using partition filter on ts and handling null country.

How to approach (SQL)

Break the prompt into steps:

  • Identify source tables, required joins, and filters (especially on time partitions).
  • Decide where you need GROUP BY vs. window functions (e.g., ROW_NUMBER, SUM() OVER, COUNT() OVER).
  • For BigQuery, think about partitioning and clustering to avoid unnecessary full scans.
  • Write the query in stages (CTEs) so each step is easy to debug and reason about.

Finish by checking edge cases: nulls, late events, duplicated keys, and extreme values.

Show SQL solution (BigQuery)

Table metadata

-- Partitioned by date(ts) for efficient time filtering
CREATE TABLE transactions (
  user_id    INT64,
  ts         TIMESTAMP,
  amount     NUMERIC
)
PARTITION BY DATE(ts);

CREATE TABLE dim_users (
  user_id INT64,
  country STRING
);

Sample data

INSERT INTO dim_users (user_id, country) VALUES
  (1, 'US'),
  (2, 'US'),
  (3, 'IN'),
  (4, NULL);  -- unknown country

INSERT INTO transactions (user_id, ts, amount) VALUES
  (1, TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 5 DAY), 100),
  (1, TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 2 DAY), 50),
  (2, TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 3 DAY), 200),
  (3, TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 10 DAY), 300),
  (4, TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 1 DAY), 400);

Query: top 3 countries by amount in last 30 days

WITH recent_tx AS (
  SELECT
    t.user_id,
    t.amount,
    d.country
  FROM
    transactions AS t
  LEFT JOIN
    dim_users AS d
  USING (user_id)
  WHERE
    -- Partition filter to limit scanned partitions
    DATE(t.ts) >= DATE_SUB(CURRENT_DATE(), INTERVAL 30 DAY)
),
country_totals AS (
  SELECT
    IFNULL(country, 'UNKNOWN') AS country,
    SUM(amount) AS total_amount
  FROM
    recent_tx
  GROUP BY
    country
),
ranked AS (
  SELECT
    country,
    total_amount,
    ROW_NUMBER() OVER (ORDER BY total_amount DESC) AS rn
  FROM
    country_totals
)
SELECT
  country,
  total_amount
FROM
  ranked
WHERE
  rn <= 3
ORDER BY
  total_amount DESC;