Day 79 — Decode Ways
Coding problem
| Problem | Decode Ways |
| LeetCode ID(s) | LeetCode #91 |
| Difficulty | Medium |
| Pattern | DP |
| Company tags | |
| Suggested time | 20m |
Solution outline (coding)
dp[i]= ways to decodes[0:i]; single digit and double digit if valid.- Handle leading zero and
06vs6rules. - Rolling two values can reduce space.
Time complexity: O(n).
Space complexity: O(n) or O(1) rolling.
Show Python solution
class Solution:
def numDecodings(self, s: str) -> int:
if not s or s[0] == '0':
return 0
n = len(s)
dp = [0] * (n + 1)
dp[0] = dp[1] = 1
for i in range(2, n + 1):
if s[i - 1] != '0':
dp[i] += dp[i - 1]
two = int(s[i - 2 : i])
if 10 <= two <= 26:
dp[i] += dp[i - 2]
return dp[n]
# Input: s = "226"
# Output: 3SQL interview practice
1. Interview question
Companies / track: Google
Google: SQL screens usually assume BigQuery, Ads / Search / YouTube-style fact tables, and talking through bytes processed and partition pruning.
What you are asked to write (SQL prompt):
Frame this as metrics work for **Google**-scale surfaces (ads, product, or engagement — as the tables suggest).
messages(user_id, ts, encoded_string). BigQuery: implement DP-like decode count in UDF per encoded_string and aggregate fail vs success rates.
Tables implied by the prompt:
messages(user_id, ts, encoded_string)
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 DP to SQL: say the relational equivalent (e.g. hash map →
GROUP BY+ key; two pointers → ordered window + filter). - Aggregate: fix
GROUP BYkeys, thenHAVINGfor thresholds (e.g. min volume). - Arrays / UDFs:
UNNESTand offsets for index; say when logic belongs in SQL vs a UDF. - Rates:
SAFE_DIVIDEorNULLIF; define numerator and denominator. - Structure: CTEs (
WITH) — one step per CTE; validate on a tiny slice (counts, nulls, duplicates).
Show SQL solution (BigQuery)
Main query
SELECT user_id, encoded_string, LENGTH(encoded_string) AS enc_len FROM messages;