Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Added One sample Z-test question
  • Loading branch information
Jeet009 committed Nov 11, 2025
commit 2479187b7dc2293f53c8fc8a85b67adf6a6c09d5
18 changes: 18 additions & 0 deletions questions/186_one-sample-z-test-hypothesis-testing/description.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Implement a function to perform a one-sample Z-test for a population mean when the population standard deviation is known. Your function must support both one-tailed and two-tailed alternatives.

Implement a function with the signature:
- one_sample_z_test(sample_mean, population_mean, population_std, n, alternative="two-sided")

Where:
- sample_mean: The observed sample mean (float)
- population_mean: The hypothesized population mean under H0 (float)
- population_std: The known population standard deviation (float > 0)
- n: Sample size (int > 0)
- alternative: One of {"two-sided", "greater", "less"}

Return a dictionary with:
- "z": the computed Z statistic rounded to 4 decimals
- "p_value": the corresponding p-value rounded to 4 decimals

Use the standard normal distribution for the p-value. Handle invalid inputs minimally by assuming valid types and values.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"input": "sample_mean=103.0, population_mean=100.0, population_std=15.0, n=36, alternative='greater'",
"output": "{'z': 1.2, 'p_value': 0.1151}",
"reasoning": "Standard error = 15/sqrt(36)=2.5. Z=(103-100)/2.5=1.2. For a 'greater' test, p=1-CDF(1.2)=0.1151."
}

20 changes: 20 additions & 0 deletions questions/186_one-sample-z-test-hypothesis-testing/learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
A one-sample Z-test assesses whether the mean of a population differs from a hypothesized value when the population standard deviation is known. It is appropriate for large samples (by CLT) or when normality is assumed and the population standard deviation is known.

Test statistic:
- z = (x̄ − μ0) / (σ / √n)
- x̄: sample mean
- μ0: hypothesized mean under H0
- σ: known population standard deviation
- n: sample size

P-value computation uses the standard normal distribution:
- Two-sided (H1: μ ≠ μ0): p = 2 · min(Φ(z), 1 − Φ(z))
- Right-tailed (H1: μ > μ0): p = 1 − Φ(z)
- Left-tailed (H1: μ < μ0): p = Φ(z)

Decision at level α:
- Reject H0 if p ≤ α; otherwise, fail to reject H0.

Notes:
- If σ is unknown, use a one-sample t-test with the sample standard deviation instead.

13 changes: 13 additions & 0 deletions questions/186_one-sample-z-test-hypothesis-testing/meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"id": "186",
"title": "One-Sample Z-Test for Mean (One and Two-Tailed)",
"difficulty": "easy",
"category": "Statistics",
"video": "",
"likes": "0",
"dislikes": "0",
"contributor": [
{ "profile_link": "https://github.com/Jeet009", "name": "Jeet Mukherjee" }
]
}

40 changes: 40 additions & 0 deletions questions/186_one-sample-z-test-hypothesis-testing/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from math import erf, sqrt

def _standard_normal_cdf(x):
return 0.5 * (1.0 + erf(x / sqrt(2.0)))

def one_sample_z_test(sample_mean, population_mean, population_std, n, alternative="two-sided"):
"""
Perform a one-sample Z-test for a population mean with known population std.

Parameters
----------
sample_mean : float
population_mean : float
population_std : float
n : int
alternative : str
One of {"two-sided", "greater", "less"}

Returns
-------
dict with keys:
- "z": Z-statistic rounded to 4 decimals
- "p_value": p-value rounded to 4 decimals
"""
standard_error = population_std / sqrt(n)
z = (sample_mean - population_mean) / standard_error
cdf = _standard_normal_cdf(z)

if alternative == "two-sided":
p = 2.0 * min(cdf, 1.0 - cdf)
elif alternative == "greater":
p = 1.0 - cdf
elif alternative == "less":
p = cdf
else:
# Fallback to two-sided if unexpected input
p = 2.0 * min(cdf, 1.0 - cdf)

return {"z": round(z, 4), "p_value": round(p, 4)}

33 changes: 33 additions & 0 deletions questions/186_one-sample-z-test-hypothesis-testing/starter_code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from math import erf, sqrt

def _standard_normal_cdf(x):
return 0.5 * (1.0 + erf(x / sqrt(2.0)))

def one_sample_z_test(sample_mean, population_mean, population_std, n, alternative="two-sided"):
"""
Perform a one-sample Z-test for a population mean with known population std.

Parameters
----------
sample_mean : float
population_mean : float
population_std : float
n : int
alternative : str
One of {"two-sided", "greater", "less"}

Returns
-------
dict with keys:
- "z": Z-statistic rounded to 4 decimals
- "p_value": p-value rounded to 4 decimals
"""
# TODO: Implement the Z statistic and p-value computation
# z = (sample_mean - population_mean) / (population_std / sqrt(n))
# Use _standard_normal_cdf for CDF of standard normal.
# For alternative:
# - "two-sided": p = 2 * min(P(Z<=z), P(Z>=z)) = 2 * min(cdf(z), 1-cdf(z))
# - "greater": p = 1 - cdf(z)
# - "less": p = cdf(z)
return {"z": 0.0, "p_value": 1.0}

27 changes: 27 additions & 0 deletions questions/186_one-sample-z-test-hypothesis-testing/tests.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[
{
"test": "one_sample_z_test(103.0, 100.0, 15.0, 36, alternative='two-sided')",
"expected_output": "{'z': 1.2, 'p_value': 0.2296}"
},
{
"test": "one_sample_z_test(103.0, 100.0, 15.0, 36, alternative='greater')",
"expected_output": "{'z': 1.2, 'p_value': 0.1151}"
},
{
"test": "one_sample_z_test(103.0, 100.0, 15.0, 36, alternative='less')",
"expected_output": "{'z': 1.2, 'p_value': 0.8849}"
},
{
"test": "one_sample_z_test(97.0, 100.0, 10.0, 25, alternative='two-sided')",
"expected_output": "{'z': -1.5, 'p_value': 0.1336}"
},
{
"test": "one_sample_z_test(97.0, 100.0, 10.0, 25, alternative='less')",
"expected_output": "{'z': -1.5, 'p_value': 0.0668}"
},
{
"test": "one_sample_z_test(97.0, 100.0, 10.0, 25, alternative='greater')",
"expected_output": "{'z': -1.5, 'p_value': 0.9332}"
}
]

63 changes: 0 additions & 63 deletions questions/188_model-versioning-system/description.md

This file was deleted.

5 changes: 0 additions & 5 deletions questions/188_model-versioning-system/example.json

This file was deleted.

126 changes: 0 additions & 126 deletions questions/188_model-versioning-system/learn.md

This file was deleted.

17 changes: 0 additions & 17 deletions questions/188_model-versioning-system/meta.json

This file was deleted.

Loading