One Sample T-Test

Prob. and Stats

Difficulty: 2 | Problem written by Junaid Ahmed
Problem reported in interviews at


Write a Python function to implement a one-sample t-test. The function should return a tuple containing the t-statistic and the p-value.

A one-sample t-test is used to determine if a population's mean is equal to a given value. The test can be used with continuous data. 

The equation for a one-sample t-test is:

\(Sample = \frac{x-\upsilon }{ ( \frac{s}{\sqrt{n}} ) }\)

x is the sample mean

u is the hypothesized mean

s is the sample standard deviation

n is the sample size

You should reject the null hypothesis if the p-value for the test is less than your chosen significance level.

To calculate the p-value we first have to find out the degrees of freedom of n samples.

We can also calculate this by using the formula:

\(df = n-1\)

You can convert this into a p-value by dividing the degree of freedom with the sample value. 



Sample Input:
<class 'int'>
x: 10
<class 'int'>
u: 15
<class 'int'>
n: 4
<class 'float'>
s: 8.5

Expected Output:
<class 'tuple'>
(-1.1764705882352942, 0.75)

This is a premium problem, to view more details of this problem please sign up for MLPro Premium. MLPro premium offers access to actual machine learning and data science interview questions and coding challenges commonly asked at tech companies all over the world

MLPro Premium also allows you to access all our high quality MCQs which are not available on the free tier.

Not able to solve a problem? MLPro premium brings you access to solutions for all problems available on MLPro

Get access to Premium only exclusive educational content available to only Premium users.

Have an issue, the MLPro support team is available 24X7 to Premium users.

This is a premium feature.
To access this and other such features, click on upgrade below.

Log in to post a comment


Input Test Case

Please enter only one test case at a time
numpy has been already imported as np (import numpy as np)