-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseed_data.py
More file actions
484 lines (448 loc) · 19.4 KB
/
Copy pathseed_data.py
File metadata and controls
484 lines (448 loc) · 19.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
"""
Seed script to populate database with initial tasks and thinking drills
Run this after setting up your .env file
"""
from motor.motor_asyncio import AsyncIOMotorClient
from datetime import datetime
from config import settings
import asyncio
# Connect to MongoDB
client = AsyncIOMotorClient(settings.mongodb_url)
db = client[settings.database_name]
async def seed_tasks():
"""Seed initial tasks for all roles"""
tasks = [
# Backend Engineer Tasks
{
"title": "Design a Rate Limiter",
"description": "Design a scalable rate limiting system for an API",
"role": "Backend Engineer",
"difficulty": "intermediate",
"estimated_time_minutes": 30,
"scenario": """You're building a public API that needs to limit requests per user.
Requirements:
- 100 requests per minute per user
- 1000 requests per hour per user
- Should be distributed across multiple servers
- Must handle 10,000 concurrent users
- Minimal latency impact (<5ms)
Your API currently handles 50,000 requests/second across 20 servers.""",
"prompts": [
"What storage mechanism will you use for tracking limits?",
"How will you handle distributed rate limiting across servers?",
"What happens when a user exceeds their limit?",
"How will you prevent race conditions?",
"What are the failure modes and how will you handle them?"
],
"created_at": datetime.utcnow()
},
{
"title": "Debug Production Incident",
"description": "Investigate and resolve a sudden spike in API latency",
"role": "Backend Engineer",
"difficulty": "advanced",
"estimated_time_minutes": 45,
"scenario": """Your API latency suddenly spiked from 50ms to 2000ms for 30% of requests.
Observations:
- Started 10 minutes ago
- Database queries look normal
- CPU and memory usage are fine
- Error rate hasn't increased
- Mostly affecting POST /orders endpoint
- GET requests are unaffected
What's your debugging approach?""",
"prompts": [
"What would you check first and why?",
"What metrics and logs would you examine?",
"What are your top 3 hypotheses?",
"How would you verify each hypothesis?",
"What immediate mitigation steps would you take?"
],
"created_at": datetime.utcnow()
},
{
"title": "Design Notification System",
"description": "Design a system to send millions of push notifications",
"role": "Backend Engineer",
"difficulty": "advanced",
"estimated_time_minutes": 40,
"scenario": """Build a notification system that sends:
- Email notifications
- Push notifications (mobile)
- SMS (for critical alerts)
Requirements:
- 5 million users
- Peak: 100,000 notifications/minute
- Delivery within 30 seconds
- Handle retries for failures
- Track delivery status
- Users can set preferences""",
"prompts": [
"What's your high-level architecture?",
"How will you queue and process notifications?",
"How will you handle different notification types?",
"How will you retry failed deliveries?",
"What happens if a third-party service (e.g., email provider) is down?"
],
"created_at": datetime.utcnow()
},
# Frontend Engineer Tasks
{
"title": "Optimize React Performance",
"description": "Improve performance of a slow dashboard component",
"role": "Frontend Engineer",
"difficulty": "intermediate",
"estimated_time_minutes": 30,
"scenario": """You have a dashboard that renders a table with 10,000 rows.
Problems:
- Takes 3 seconds to render initially
- Scrolling is janky (low FPS)
- Filtering causes entire page to freeze for 2 seconds
- Each row has 10 columns with formatted data
- Users can sort, filter, and export data
Current implementation uses simple map() over all rows.""",
"prompts": [
"What performance issues do you identify?",
"What optimization techniques would you apply?",
"How would you implement virtualization?",
"How would you handle filtering without blocking the UI?",
"What trade-offs exist between different solutions?"
],
"created_at": datetime.utcnow()
},
{
"title": "Design Offline-First Mobile App",
"description": "Design offline functionality for a note-taking app",
"role": "Frontend Engineer",
"difficulty": "advanced",
"estimated_time_minutes": 35,
"scenario": """Build an offline-first note-taking app where:
- Users can create/edit notes without internet
- Changes sync when connection returns
- Multiple devices can edit the same note
- Conflicts must be resolved
- Should feel instant (no loading spinners)
Average user has 500 notes, each 1-5KB.""",
"prompts": [
"What local storage strategy will you use?",
"How will you sync data when online?",
"How will you handle conflicts?",
"What's your data model for tracking changes?",
"How will you handle the initial load and subsequent updates?"
],
"created_at": datetime.utcnow()
},
{
"title": "Build Real-Time Collaboration",
"description": "Add real-time editing to a document editor",
"role": "Frontend Engineer",
"difficulty": "advanced",
"estimated_time_minutes": 40,
"scenario": """Add Google Docs-style collaboration to your text editor:
- Multiple users can edit simultaneously
- See other users' cursors
- Changes appear instantly
- Handle conflicts
- Show who's viewing/editing
- Works with 100+ concurrent users per document""",
"prompts": [
"What protocol/technology will you use for real-time updates?",
"How will you handle concurrent edits?",
"What's your conflict resolution strategy?",
"How will you optimize for low latency?",
"What happens when a user disconnects?"
],
"created_at": datetime.utcnow()
},
# Systems Engineer Tasks
{
"title": "Design Cache Strategy",
"description": "Design a multi-layer caching strategy",
"role": "Systems Engineer",
"difficulty": "intermediate",
"estimated_time_minutes": 30,
"scenario": """Your e-commerce site needs caching:
- 1 million products
- Product data rarely changes
- User sessions need fast access
- High read-to-write ratio (1000:1)
- Global users (multiple regions)
- Current: Database can handle 5000 QPS, need 50,000 QPS""",
"prompts": [
"What layers of caching will you implement?",
"What caching technology will you use for each layer?",
"What's your cache invalidation strategy?",
"How will you handle cache stampede?",
"What metrics will you monitor?"
],
"created_at": datetime.utcnow()
},
{
"title": "Design Database Sharding",
"description": "Shard a growing PostgreSQL database",
"role": "Systems Engineer",
"difficulty": "advanced",
"estimated_time_minutes": 45,
"scenario": """Your single PostgreSQL database is at 2TB and growing:
- User table: 50M rows
- Orders table: 500M rows
- Products table: 5M rows
- Write queries are getting slow
- Some joins across tables are needed
- Zero-downtime migration required""",
"prompts": [
"What sharding strategy will you use?",
"What's your shard key and why?",
"How will you handle cross-shard queries?",
"What's your migration plan?",
"How will you rebalance shards as data grows?"
],
"created_at": datetime.utcnow()
},
{
"title": "Handle Traffic Spike",
"description": "Prepare infrastructure for 10x traffic surge",
"role": "Systems Engineer",
"difficulty": "advanced",
"estimated_time_minutes": 35,
"scenario": """Your app will be featured on national TV in 3 days:
- Current: 10,000 requests/second
- Expected: 100,000 requests/second spike for 2 hours
- Must maintain <200ms response time
- Current setup: 10 app servers, 2 DB replicas, 1 Redis instance
- Budget: $5000 for scaling""",
"prompts": [
"What components are likely bottlenecks?",
"How will you scale each layer?",
"What auto-scaling policies will you set?",
"What's your fallback plan if things fail?",
"How will you test this before the event?"
],
"created_at": datetime.utcnow()
},
# Data Engineer Tasks
{
"title": "Design Data Pipeline",
"description": "Build a real-time analytics pipeline",
"role": "Data Engineer",
"difficulty": "intermediate",
"estimated_time_minutes": 35,
"scenario": """Build a pipeline for real-time user behavior analytics:
- Ingest: 50,000 events/second
- Process: Aggregate by user, session, page
- Store: Query latency < 100ms
- Retention: Raw data for 30 days, aggregates for 1 year
- Use cases: Dashboards, user segmentation, A/B testing""",
"prompts": [
"What technologies will you use for ingestion?",
"How will you process and transform the data?",
"What storage solutions will you use?",
"How will you handle late-arriving data?",
"What's your strategy for schema evolution?"
],
"created_at": datetime.utcnow()
},
{
"title": "Optimize Slow Query",
"description": "Fix a dashboard query taking 30 seconds",
"role": "Data Engineer",
"difficulty": "intermediate",
"estimated_time_minutes": 30,
"scenario": """A dashboard query takes 30 seconds to run:
```sql
SELECT
date,
COUNT(DISTINCT user_id) as users,
SUM(revenue) as revenue,
AVG(session_duration) as avg_duration
FROM events
WHERE date >= '2024-01-01'
AND event_type IN ('purchase', 'view')
GROUP BY date, country, device_type
ORDER BY date DESC
```
Table has 10 billion rows, growing by 50M/day.""",
"prompts": [
"What's causing the slow performance?",
"What indexes would help?",
"Would partitioning help? How would you partition?",
"Should you pre-aggregate some data?",
"What trade-offs exist in your solution?"
],
"created_at": datetime.utcnow()
},
{
"title": "Build Data Quality System",
"description": "Ensure data quality in your pipeline",
"role": "Data Engineer",
"difficulty": "advanced",
"estimated_time_minutes": 35,
"scenario": """Your data pipeline has quality issues:
- Duplicate records appearing
- Missing required fields
- Invalid data formats
- Schema mismatches between sources
- Downstream teams losing trust in data
Need to implement data quality checks.""",
"prompts": [
"What quality checks will you implement?",
"Where in the pipeline will you add validation?",
"How will you handle data that fails validation?",
"How will you monitor and alert on quality issues?",
"What's your strategy for fixing historical bad data?"
],
"created_at": datetime.utcnow()
}
]
# Clear existing tasks
await db.tasks.delete_many({})
# Insert new tasks
result = await db.tasks.insert_many(tasks)
print(f"✅ Inserted {len(result.inserted_ids)} tasks")
async def seed_drills():
"""Seed thinking drills"""
drills = [
# Spot Assumptions
{
"title": "API Design Assumptions",
"drill_type": "spot_assumptions",
"question": """A developer says: "We'll use REST API because it's the standard."
What assumption is being made?""",
"options": [
"REST is always the best choice",
"The use case fits REST well",
"The team knows REST",
"All of the above"
],
"correct_answer": "All of the above",
"explanation": "The developer assumes REST is suitable without evaluating if GraphQL, gRPC, or WebSockets might be better for the specific use case. They also assume team familiarity and that 'standard' means 'best'.",
"created_at": datetime.utcnow()
},
{
"title": "Database Choice",
"drill_type": "spot_assumptions",
"question": """Team decides: "Let's use MongoDB because we need to scale."
What's the flawed assumption?""",
"options": [
"NoSQL automatically scales better",
"Relational databases can't scale",
"Scaling is only about database choice",
"All of the above"
],
"correct_answer": "All of the above",
"explanation": "This assumes NoSQL is always more scalable, ignores that relational databases can scale horizontally, and oversimplifies scaling to just database choice when architecture matters more.",
"created_at": datetime.utcnow()
},
# Rank Failures
{
"title": "Payment System Failures",
"drill_type": "rank_failures",
"question": """Rank these failure modes for a payment system from MOST to LEAST critical:
A) User charged twice
B) Slow checkout (5 seconds)
C) Payment successful but order not created
D) Payment gateway timeout""",
"options": [
"C, A, D, B",
"A, C, D, B",
"D, C, A, B",
"B, D, A, C"
],
"correct_answer": "C, A, D, B",
"explanation": "C is worst (money taken, no order = angry customers + refunds). A is bad (financial loss + trust issues). D is recoverable with retry. B is annoying but not critical.",
"created_at": datetime.utcnow()
},
{
"title": "Social Media Feed Failures",
"drill_type": "rank_failures",
"question": """Rank these issues for a social media feed from MOST to LEAST critical:
A) Feed loads slowly (3 seconds)
B) Same post shown multiple times
C) User's own posts not visible
D) Can't refresh feed (temporary)""",
"options": [
"C, B, A, D",
"D, C, B, A",
"C, D, A, B",
"B, C, D, A"
],
"correct_answer": "C, D, A, B",
"explanation": "C breaks core value (seeing your content). D prevents refreshing but cached content works. A annoys but works. B is a UX issue but feed functions.",
"created_at": datetime.utcnow()
},
# Predict Scaling
{
"title": "Session Storage Scaling",
"drill_type": "predict_scaling",
"question": """You store user sessions in memory on each server. Current: 10,000 users, 5 servers. What breaks first when you hit 1M users?""",
"options": [
"Memory on each server",
"Session inconsistency across servers",
"CPU from session lookups",
"Network bandwidth"
],
"correct_answer": "Session inconsistency across servers",
"explanation": "With in-memory sessions, a user can be routed to different servers, losing their session. This breaks before memory limits. You need sticky sessions or centralized session storage (Redis).",
"created_at": datetime.utcnow()
},
{
"title": "Notification Queue",
"drill_type": "predict_scaling",
"question": """You use a single-threaded worker to send email notifications. Currently 1000 emails/hour. What happens at 100,000 emails/hour?""",
"options": [
"Memory overflow from queue",
"Emails delayed by hours",
"Database connection exhaustion",
"Email provider blocks you"
],
"correct_answer": "Emails delayed by hours",
"explanation": "A single worker can only process so fast. At 100x load, the queue grows faster than it's processed, causing hour-long delays. You need multiple workers or a better queuing system.",
"created_at": datetime.utcnow()
},
# Trade-offs
{
"title": "Cache Trade-off",
"drill_type": "choose_tradeoffs",
"question": """For caching product prices, what's the best trade-off?
A) Cache for 1 hour (fast, might show stale prices)
B) Cache for 1 minute (slower, more accurate)
C) No cache (always accurate, slow)
D) Cache forever, invalidate on update (complex)""",
"options": [
"A - Speed over accuracy",
"B - Balanced approach",
"C - Accuracy over speed",
"D - Best of both worlds"
],
"correct_answer": "B - Balanced approach",
"explanation": "1-minute cache is best for most e-commerce. Prices don't change often, so staleness is minimal. 1 hour risks showing wrong prices during sales. No cache overloads DB. Option D adds complexity that's usually not worth it.",
"created_at": datetime.utcnow()
},
{
"title": "Consistency vs Availability",
"drill_type": "choose_tradeoffs",
"question": """For a social media 'like' counter, which trade-off makes sense?""",
"options": [
"Strong consistency - Exact count always",
"Eventual consistency - Approximate count, high availability",
"Weak consistency - Cached count, may be very stale",
"No counting - Too complex"
],
"correct_answer": "Eventual consistency - Approximate count, high availability",
"explanation": "Like counts don't need to be exact. Users won't notice if 1,234 likes shows as 1,230 temporarily. Eventual consistency allows high availability and scale. Strong consistency would be too expensive for this feature.",
"created_at": datetime.utcnow()
}
]
# Clear existing drills
await db.drills.delete_many({})
# Insert new drills
result = await db.drills.insert_many(drills)
print(f"✅ Inserted {len(result.inserted_ids)} thinking drills")
async def main():
print("🌱 Seeding database...")
await seed_tasks()
await seed_drills()
print("✅ Database seeded successfully!")
if __name__ == "__main__":
asyncio.run(main())
client.close()