Skip to content

Commit 17e668e

Browse files
feat: enhance ML Model Monitoring system design answer with detailed architecture, scale requirements, and Python implementation.
1 parent 1fd7cd0 commit 17e668e

File tree

12 files changed

+4211
-569
lines changed

12 files changed

+4211
-569
lines changed

docs/Interview-Questions/AB-testing.md

Lines changed: 67 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,73 +1334,88 @@ This document provides a curated list of A/B Testing and Experimentation intervi
13341334
## Code Examples
13351335

13361336
### 1. Power Analysis and Sample Size (Python)
1337-
Calculating the required sample size before starting an experiment.
13381337

1339-
```python
1340-
from statsmodels.stats.power import TTestIndPower
1341-
import numpy as np
1338+
??? success "View Code Example"
13421339

1343-
# Parameters
1344-
effect_size = 0.1 # Cohen's d (Standardized difference)
1345-
alpha = 0.05 # Significance level (5%)
1346-
power = 0.8 # Power (80%)
13471340

1348-
analysis = TTestIndPower()
1349-
sample_size = analysis.solve_power(effect_size=effect_size, power=power, alpha=alpha)
1341+
**Difficulty:** 🟢 Easy | **Tags:** `Code Example` | **Asked by:** Code Pattern
1342+
Calculating the required sample size before starting an experiment.
13501343

1351-
print(f"Required sample size per group: {int(np.ceil(sample_size))}")
1352-
```
1344+
```python
1345+
from statsmodels.stats.power import TTestIndPower
1346+
import numpy as np
1347+
1348+
# Parameters
1349+
effect_size = 0.1 # Cohen's d (Standardized difference)
1350+
alpha = 0.05 # Significance level (5%)
1351+
power = 0.8 # Power (80%)
1352+
1353+
analysis = TTestIndPower()
1354+
sample_size = analysis.solve_power(effect_size=effect_size, power=power, alpha=alpha)
1355+
1356+
print(f"Required sample size per group: {int(np.ceil(sample_size))}")
1357+
```
13531358

13541359
### 2. Bayesian A/B Test (Beta-Binomial)
1355-
Updating beliefs about conversion rates.
13561360

1357-
```python
1358-
from scipy.stats import beta
1361+
??? success "View Code Example"
13591362

1360-
# Prior: Uniform distribution (Beta(1,1))
1361-
alpha_prior = 1
1362-
beta_prior = 1
13631363

1364-
# Data: Group A
1365-
conversions_A = 120
1366-
failures_A = 880
1364+
**Difficulty:** 🟢 Easy | **Tags:** `Code Example` | **Asked by:** Code Pattern
1365+
Updating beliefs about conversion rates.
13671366

1368-
# Data: Group B
1369-
conversions_B = 140
1370-
failures_B = 860
1367+
```python
1368+
from scipy.stats import beta
1369+
1370+
# Prior: Uniform distribution (Beta(1,1))
1371+
alpha_prior = 1
1372+
beta_prior = 1
1373+
1374+
# Data: Group A
1375+
conversions_A = 120
1376+
failures_A = 880
13711377

1372-
# Posterior
1373-
posterior_A = beta(alpha_prior + conversions_A, beta_prior + failures_A)
1374-
posterior_B = beta(alpha_prior + conversions_B, beta_prior + failures_B)
1378+
# Data: Group B
1379+
conversions_B = 140
1380+
failures_B = 860
13751381

1376-
# Probability B > A (Approximate via simulation)
1377-
samples = 100000
1378-
prob_b_better = (posterior_B.rvs(samples) > posterior_A.rvs(samples)).mean()
1382+
# Posterior
1383+
posterior_A = beta(alpha_prior + conversions_A, beta_prior + failures_A)
1384+
posterior_B = beta(alpha_prior + conversions_B, beta_prior + failures_B)
13791385

1380-
print(f"Probability B is better than A: {prob_b_better:.4f}")
1381-
```
1386+
# Probability B > A (Approximate via simulation)
1387+
samples = 100000
1388+
prob_b_better = (posterior_B.rvs(samples) > posterior_A.rvs(samples)).mean()
1389+
1390+
print(f"Probability B is better than A: {prob_b_better:.4f}")
1391+
```
13821392

13831393
### 3. Bootstrap Confidence Interval
1384-
Calculating CI for non-normal metrics (e.g., Revenue per User).
1385-
1386-
```python
1387-
import numpy as np
1388-
1389-
data_control = np.random.lognormal(mean=2, sigma=1, size=1000)
1390-
data_variant = np.random.lognormal(mean=2.1, sigma=1, size=1000)
1391-
1392-
def bootstrap_mean_diff(data1, data2, n_bootstrap=1000):
1393-
diffs = []
1394-
for _ in range(n_bootstrap):
1395-
# Sample with replacement
1396-
sample1 = np.random.choice(data1, len(data1), replace=True)
1397-
sample2 = np.random.choice(data2, len(data2), replace=True)
1398-
diffs.append(sample2.mean() - sample1.mean())
1399-
return np.percentile(diffs, [2.5, 97.5])
1400-
1401-
ci = bootstrap_mean_diff(data_control, data_variant)
1402-
print(f"95% CI for difference: {ci}")
1403-
```
1394+
1395+
??? success "View Code Example"
1396+
1397+
1398+
**Difficulty:** 🟢 Easy | **Tags:** `Code Example` | **Asked by:** Code Pattern
1399+
Calculating CI for non-normal metrics (e.g., Revenue per User).
1400+
1401+
```python
1402+
import numpy as np
1403+
1404+
data_control = np.random.lognormal(mean=2, sigma=1, size=1000)
1405+
data_variant = np.random.lognormal(mean=2.1, sigma=1, size=1000)
1406+
1407+
def bootstrap_mean_diff(data1, data2, n_bootstrap=1000):
1408+
diffs = []
1409+
for _ in range(n_bootstrap):
1410+
# Sample with replacement
1411+
sample1 = np.random.choice(data1, len(data1), replace=True)
1412+
sample2 = np.random.choice(data2, len(data2), replace=True)
1413+
diffs.append(sample2.mean() - sample1.mean())
1414+
return np.percentile(diffs, [2.5, 97.5])
1415+
1416+
ci = bootstrap_mean_diff(data_control, data_variant)
1417+
print(f"95% CI for difference: {ci}")
1418+
```
14041419

14051420
---
14061421

docs/Interview-Questions/LangChain.md

Lines changed: 75 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1473,79 +1473,94 @@ This is updated frequently but right now this is the most exhaustive list of typ
14731473
## Code Examples
14741474

14751475
### 1. Basic RAG Pipeline with LCEL
1476-
```python
1477-
from langchain_community.vectorstores import FAISS
1478-
from langchain_core.output_parsers import StrOutputParser
1479-
from langchain_core.prompts import ChatPromptTemplate
1480-
from langchain_core.runnables import RunnablePassthrough
1481-
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
1482-
1483-
vectorstore = FAISS.from_texts(["harrison worked at kensho"], embedding=OpenAIEmbeddings())
1484-
retriever = vectorstore.as_retriever()
1485-
template = """Answer the question based only on the following context:
1486-
{context}
1487-
1488-
Question: {question}
1489-
"""
1490-
prompt = ChatPromptTemplate.from_template(template)
1491-
model = ChatOpenAI()
1492-
1493-
retrieval_chain = (
1494-
{"context": retriever, "question": RunnablePassthrough()}
1495-
| prompt
1496-
| model
1497-
| StrOutputParser()
1498-
)
1499-
1500-
retrieval_chain.invoke("where did harrison work?")
1501-
```
1476+
1477+
??? success "View Code Example"
1478+
1479+
1480+
**Difficulty:** 🟢 Easy | **Tags:** `Code Example` | **Asked by:** Code Pattern
1481+
```python
1482+
from langchain_community.vectorstores import FAISS
1483+
from langchain_core.output_parsers import StrOutputParser
1484+
from langchain_core.prompts import ChatPromptTemplate
1485+
from langchain_core.runnables import RunnablePassthrough
1486+
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
1487+
1488+
vectorstore = FAISS.from_texts(["harrison worked at kensho"], embedding=OpenAIEmbeddings())
1489+
retriever = vectorstore.as_retriever()
1490+
template = """Answer the question based only on the following context:
1491+
{context}
1492+
1493+
Question: {question}
1494+
"""
1495+
prompt = ChatPromptTemplate.from_template(template)
1496+
model = ChatOpenAI()
1497+
1498+
retrieval_chain = (
1499+
{"context": retriever, "question": RunnablePassthrough()}
1500+
| prompt
1501+
| model
1502+
| StrOutputParser()
1503+
)
1504+
1505+
retrieval_chain.invoke("where did harrison work?")
1506+
```
15021507

15031508
### 2. Custom Agent with Tool Use
1504-
```python
1505-
from langchain.agents import tool
1506-
from langchain_openai import ChatOpenAI
1507-
from langchain.agents import AgentExecutor, create_tool_calling_agent
1508-
from langchain_core.prompts import ChatPromptTemplate
15091509

1510-
@tool
1511-
def multiply(first_int: int, second_int: int) -> int:
1512-
"""Multiply two integers together."""
1513-
return first_int * second_int
1510+
??? success "View Code Example"
1511+
15141512

1515-
tools = [multiply]
1516-
llm = ChatOpenAI(model="gpt-3.5-turbo-0125")
1513+
**Difficulty:** 🟢 Easy | **Tags:** `Code Example` | **Asked by:** Code Pattern
1514+
```python
1515+
from langchain.agents import tool
1516+
from langchain_openai import ChatOpenAI
1517+
from langchain.agents import AgentExecutor, create_tool_calling_agent
1518+
from langchain_core.prompts import ChatPromptTemplate
15171519

1518-
prompt = ChatPromptTemplate.from_messages([
1519-
("system", "You are a helpful assistant"),
1520-
("user", "{input}"),
1521-
("placeholder", "{agent_scratchpad}"),
1522-
])
1520+
@tool
1521+
def multiply(first_int: int, second_int: int) -> int:
1522+
"""Multiply two integers together."""
1523+
return first_int * second_int
15231524

1524-
agent = create_tool_calling_agent(llm, tools, prompt)
1525-
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
1525+
tools = [multiply]
1526+
llm = ChatOpenAI(model="gpt-3.5-turbo-0125")
15261527

1527-
agent_executor.invoke({"input": "what is 5 times 8?"})
1528-
```
1528+
prompt = ChatPromptTemplate.from_messages([
1529+
("system", "You are a helpful assistant"),
1530+
("user", "{input}"),
1531+
("placeholder", "{agent_scratchpad}"),
1532+
])
1533+
1534+
agent = create_tool_calling_agent(llm, tools, prompt)
1535+
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
1536+
1537+
agent_executor.invoke({"input": "what is 5 times 8?"})
1538+
```
15291539

15301540
### 3. Structured Output Extraction
1531-
```python
1532-
from typing import List
1533-
from langchain_core.pydantic_v1 import BaseModel, Field
1534-
from langchain_openai import ChatOpenAI
15351541

1536-
class Person(BaseModel):
1537-
name: str = Field(description="The name of the person")
1538-
age: int = Field(description="The age of the person")
1542+
??? success "View Code Example"
15391543

1540-
class People(BaseModel):
1541-
people: List[Person]
15421544

1543-
llm = ChatOpenAI()
1544-
structured_llm = llm.with_structured_output(People)
1545+
**Difficulty:** 🟢 Easy | **Tags:** `Code Example` | **Asked by:** Code Pattern
1546+
```python
1547+
from typing import List
1548+
from langchain_core.pydantic_v1 import BaseModel, Field
1549+
from langchain_openai import ChatOpenAI
1550+
1551+
class Person(BaseModel):
1552+
name: str = Field(description="The name of the person")
1553+
age: int = Field(description="The age of the person")
15451554

1546-
text = "Alice is 30 years old and Bob is 25."
1547-
structured_llm.invoke(text)
1548-
```
1555+
class People(BaseModel):
1556+
people: List[Person]
1557+
1558+
llm = ChatOpenAI()
1559+
structured_llm = llm.with_structured_output(People)
1560+
1561+
text = "Alice is 30 years old and Bob is 25."
1562+
structured_llm.invoke(text)
1563+
```
15491564

15501565
---
15511566

0 commit comments

Comments
 (0)