Day 1 — Two Sum
Coding problem
| Problem | Two Sum |
| LeetCode ID(s) | LeetCode #1 |
| Difficulty | Easy |
| Pattern | Hash Map |
| Company tags | All Companies |
| Suggested time | 10m |
Solution outline (coding)
- Scan
numsonce from left to right while maintaining a hash map from value → index for elements you have already seen. - At index
i, letx = nums[i]. Iftarget - xexists in the map, return[index(target - x), i]immediately. - Otherwise store
x -> iin the map and continue—never reuse the same element twice because you only look backward.
Time complexity: O(n) — single pass over n elements.
Space complexity: O(n) — hash map holds up to n entries in the worst case.
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 interview practice
1. Interview question
Companies / track: All companies (general analytics)
Universal analytics: geography rollups, time windows, and joins to user dimensions show up in almost every big-tech SQL round—think payments, ads, or subscriptions.
What you are asked to write (SQL prompt):
Phone-screen style (any large tech company) — revenue, geo, and time windows are universal.
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.
Tables implied by the prompt:
transactions(user_id, ts, amount)dim_users(user_id, country)
Engine: BigQuery — use its date, array, and approximate functions as documented.
2. Solution outline
- Clarify out loud: result grain (one row per what?), join keys, time zone, and any
ORDER BY/LIMIT/ tie-breakers. - Map Hash Map to SQL: say the relational equivalent (e.g. hash map →
GROUP BY+ key; two pointers → ordered window + filter). - Filter time first: predicate on
DATE(ts)/ partition column before heavy joins; state the window in plain English. - Nulls:
COALESCEfor dimensions; decide how unknowns appear in the result. - Structure: CTEs (
WITH) — one step per CTE; validate on a tiny slice (counts, nulls, duplicates).
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;