#1 intial version
This commit is contained in:
@@ -0,0 +1,173 @@
|
|||||||
|
# Analysis #XX — Two Sum Is Not About Numbers
|
||||||
|
|
||||||
|
## Problem (LeetCode-style)
|
||||||
|
|
||||||
|
You are given a list of records. Each record contains:
|
||||||
|
|
||||||
|
- an identifier
|
||||||
|
- a value
|
||||||
|
- optional metadata
|
||||||
|
|
||||||
|
Your task is to find whether there exists a pair of records whose values sum to a given target.
|
||||||
|
|
||||||
|
Return the identifiers of any such pair.
|
||||||
|
|
||||||
|
Constraints:
|
||||||
|
- Each record may be used at most once
|
||||||
|
- At most one valid answer exists
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Typical Interview Thinking
|
||||||
|
|
||||||
|
1. Start with brute force:
|
||||||
|
- Check all pairs → O(n²)
|
||||||
|
|
||||||
|
2. Optimize:
|
||||||
|
- Use a hash map
|
||||||
|
- Store seen values
|
||||||
|
- Lookup complement (target - value)
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
unordered_map<int, int> seen;
|
||||||
|
|
||||||
|
for (int i = 0; i < n; ++i) {
|
||||||
|
int complement = target - nums[i];
|
||||||
|
|
||||||
|
if (seen.count(complement)) {
|
||||||
|
return {seen[complement], i};
|
||||||
|
}
|
||||||
|
|
||||||
|
seen[nums[i]] = i;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Time complexity: O(n)
|
||||||
|
Space complexity: O(n)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## What This Actually Tests
|
||||||
|
|
||||||
|
- Pattern recognition
|
||||||
|
- Familiarity with hash maps
|
||||||
|
- Knowledge of time complexity
|
||||||
|
- Prior exposure to the problem
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Real-World Version (Logs & Event Correlation)
|
||||||
|
|
||||||
|
### Synthetic Log Example
|
||||||
|
|
||||||
|
```
|
||||||
|
2026-04-16T10:15:01.123Z service=api event=parse_input latency=12ms request_id=req-1001
|
||||||
|
2026-04-16T10:15:01.130Z service=cache event=cache_miss latency=48ms request_id=req-1001
|
||||||
|
2026-04-16T10:15:01.135Z service=db event=read_user latency=55ms request_id=req-1001
|
||||||
|
2026-04-16T10:15:01.144Z service=net event=external_call latency=47ms request_id=req-1001
|
||||||
|
2026-04-16T10:15:01.151Z service=cache event=cache_miss latency=60ms request_id=req-3001
|
||||||
|
2026-04-16T10:15:01.154Z service=net event=external_call latency=52ms request_id=req-3001
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Real Problem
|
||||||
|
|
||||||
|
Detect whether there exist two events:
|
||||||
|
|
||||||
|
- belonging to the same request_id
|
||||||
|
- occurring within a time window
|
||||||
|
- whose combined latency exceeds a threshold
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Where LeetCode Logic Breaks
|
||||||
|
|
||||||
|
### 1. Not Exact Match
|
||||||
|
LeetCode:
|
||||||
|
```
|
||||||
|
a + b == target
|
||||||
|
```
|
||||||
|
|
||||||
|
Reality:
|
||||||
|
```
|
||||||
|
a + b > threshold
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 2. Context Matters (request_id)
|
||||||
|
|
||||||
|
You cannot mix unrelated events.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 3. Time Window
|
||||||
|
|
||||||
|
Events must be close in time.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 4. Streaming Data
|
||||||
|
|
||||||
|
- Data arrives continuously
|
||||||
|
- May be out of order
|
||||||
|
- Cannot store everything
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Real Engineering Approach
|
||||||
|
|
||||||
|
### Core Idea
|
||||||
|
|
||||||
|
Maintain sliding windows per request_id.
|
||||||
|
|
||||||
|
### Pseudocode
|
||||||
|
|
||||||
|
```
|
||||||
|
for each incoming event:
|
||||||
|
bucket = active_events[event.request_id]
|
||||||
|
|
||||||
|
remove old events outside time window
|
||||||
|
|
||||||
|
for each old_event in bucket:
|
||||||
|
if event.latency + old_event.latency > threshold:
|
||||||
|
report anomaly
|
||||||
|
|
||||||
|
add event to bucket
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Additional Real Constraints
|
||||||
|
|
||||||
|
- Out-of-order events
|
||||||
|
- Missing logs
|
||||||
|
- Duplicate events
|
||||||
|
- Noise filtering
|
||||||
|
- Memory limits
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Key Takeaway
|
||||||
|
|
||||||
|
Two Sum is not about numbers.
|
||||||
|
|
||||||
|
It is about recognizing patterns in controlled environments.
|
||||||
|
|
||||||
|
Real engineering problems are about:
|
||||||
|
|
||||||
|
- defining valid data
|
||||||
|
- handling imperfect inputs
|
||||||
|
- managing time and memory
|
||||||
|
- maintaining system behavior under constraints
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Project Perspective
|
||||||
|
|
||||||
|
Exists in real engineering?
|
||||||
|
→ Yes, but heavily transformed
|
||||||
|
|
||||||
|
Exists in interview form?
|
||||||
|
→ Yes, but oversimplified
|
||||||
Reference in New Issue
Block a user