60 Coding Questions for Data Science
Technical Test
Python Coding Questions
Python Question 1
Q: Write a Python program to find the factorial of a number.
Answer:
def factorial(n):
return 1 if n==0 else n * factorial(n-1)
Python Question 2
Q: Write a program to check if a string is a palindrome.
Answer:
def is_palindrome(s):
return s == s[::-1]
Python Question 3
Q: Find the largest number in a list.
Answer:
def find_max(lst):
return max(lst)
Python Question 4
Q: Sort a list using bubble sort.
Answer:
def bubble_sort(arr):
n = len(arr)
for i in range(n):
for j in range(0, n-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
Python Question 5
Q: Reverse a linked list (single).
Answer:
def reverse_linked_list(head):
prev = None
while head:
nxt = head.next
head.next = prev
prev = head
head = nxt
return prev
Python Question 6
Q: Write a Python program to find the factorial of a number.
Answer:
def factorial(n):
return 1 if n==0 else n * factorial(n-1)
Python Question 7
Q: Write a program to check if a string is a palindrome.
Answer:
def is_palindrome(s):
return s == s[::-1]
Python Question 8
Q: Find the largest number in a list.
Answer:
def find_max(lst):
return max(lst)
Python Question 9
Q: Sort a list using bubble sort.
Answer:
def bubble_sort(arr):
n = len(arr)
for i in range(n):
for j in range(0, n-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
Python Question 10
Q: Reverse a linked list (single).
Answer:
def reverse_linked_list(head):
prev = None
while head:
nxt = head.next
head.next = prev
prev = head
head = nxt
return prev
Python Question 11
Q: Write a Python program to find the factorial of a number.
Answer:
def factorial(n):
return 1 if n==0 else n * factorial(n-1)
Python Question 12
Q: Write a program to check if a string is a palindrome.
Answer:
def is_palindrome(s):
return s == s[::-1]
Python Question 13
Q: Find the largest number in a list.
Answer:
def find_max(lst):
return max(lst)
Python Question 14
Q: Sort a list using bubble sort.
Answer:
def bubble_sort(arr):
n = len(arr)
for i in range(n):
for j in range(0, n-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
Python Question 15
Q: Reverse a linked list (single).
Answer:
def reverse_linked_list(head):
prev = None
while head:
nxt = head.next
head.next = prev
prev = head
head = nxt
return prev
Python Question 16
Q: Write a Python program to find the factorial of a number.
Answer:
def factorial(n):
return 1 if n==0 else n * factorial(n-1)
Python Question 17
Q: Write a program to check if a string is a palindrome.
Answer:
def is_palindrome(s):
return s == s[::-1]
Python Question 18
Q: Find the largest number in a list.
Answer:
def find_max(lst):
return max(lst)
Python Question 19
Q: Sort a list using bubble sort.
Answer:
def bubble_sort(arr):
n = len(arr)
for i in range(n):
for j in range(0, n-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
Python Question 20
Q: Reverse a linked list (single).
Answer:
def reverse_linked_list(head):
prev = None
while head:
nxt = head.next
head.next = prev
prev = head
head = nxt
return prev
SQL Coding Questions
SQL Question 1
Q: Write a query to find the second highest salary from employees.
Answer:
SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM
employees);
SQL Question 2
Q: Find the number of employees in each department.
Answer:
SELECT department_id, COUNT(*) FROM employees GROUP BY department_id;
SQL Question 3
Q: Get employee names who joined in the year 2023.
Answer:
SELECT name FROM employees WHERE YEAR(join_date) = 2023;
SQL Question 4
Q: Get departments with more than 10 employees.
Answer:
SELECT department_id FROM employees GROUP BY department_id HAVING COUNT(*) >
10;
SQL Question 5
Q: Find all employees who do not have a manager.
Answer:
SELECT * FROM employees WHERE manager_id IS NULL;
SQL Question 6
Q: Write a query to find the second highest salary from employees.
Answer:
SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM
employees);
SQL Question 7
Q: Find the number of employees in each department.
Answer:
SELECT department_id, COUNT(*) FROM employees GROUP BY department_id;
SQL Question 8
Q: Get employee names who joined in the year 2023.
Answer:
SELECT name FROM employees WHERE YEAR(join_date) = 2023;
SQL Question 9
Q: Get departments with more than 10 employees.
Answer:
SELECT department_id FROM employees GROUP BY department_id HAVING COUNT(*) >
10;
SQL Question 10
Q: Find all employees who do not have a manager.
Answer:
SELECT * FROM employees WHERE manager_id IS NULL;
SQL Question 11
Q: Write a query to find the second highest salary from employees.
Answer:
SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM
employees);
SQL Question 12
Q: Find the number of employees in each department.
Answer:
SELECT department_id, COUNT(*) FROM employees GROUP BY department_id;
SQL Question 13
Q: Get employee names who joined in the year 2023.
Answer:
SELECT name FROM employees WHERE YEAR(join_date) = 2023;
SQL Question 14
Q: Get departments with more than 10 employees.
Answer:
SELECT department_id FROM employees GROUP BY department_id HAVING COUNT(*) >
10;
SQL Question 15
Q: Find all employees who do not have a manager.
Answer:
SELECT * FROM employees WHERE manager_id IS NULL;
SQL Question 16
Q: Write a query to find the second highest salary from employees.
Answer:
SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM
employees);
SQL Question 17
Q: Find the number of employees in each department.
Answer:
SELECT department_id, COUNT(*) FROM employees GROUP BY department_id;
SQL Question 18
Q: Get employee names who joined in the year 2023.
Answer:
SELECT name FROM employees WHERE YEAR(join_date) = 2023;
SQL Question 19
Q: Get departments with more than 10 employees.
Answer:
SELECT department_id FROM employees GROUP BY department_id HAVING COUNT(*) >
10;
SQL Question 20
Q: Find all employees who do not have a manager.
Answer:
SELECT * FROM employees WHERE manager_id IS NULL;
Machine Learning Coding Questions
Machine Question 1
Q: Train a logistic regression classifier in sklearn.
Answer:
from sklearn.linear_model import LogisticRegression
model = LogisticRegression()
model.fit(X_train, y_train)
Machine Question 2
Q: Perform k-fold cross validation using sklearn.
Answer:
from sklearn.model_selection import cross_val_score
scores = cross_val_score(model, X, y, cv=5)
Machine Question 3
Q: Plot feature importances of a decision tree.
Answer:
import matplotlib.pyplot as plt
plt.bar(range(len(model.feature_importances_)), model.feature_importances_)
Machine Question 4
Q: Normalize data using MinMaxScaler.
Answer:
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
X_scaled = scaler.fit_transform(X)
Machine Question 5
Q: Calculate accuracy, precision, recall using sklearn.
Answer:
from sklearn.metrics import accuracy_score, precision_score, recall_score
accuracy = accuracy_score(y_true, y_pred)
Machine Question 6
Q: Train a logistic regression classifier in sklearn.
Answer:
from sklearn.linear_model import LogisticRegression
model = LogisticRegression()
model.fit(X_train, y_train)
Machine Question 7
Q: Perform k-fold cross validation using sklearn.
Answer:
from sklearn.model_selection import cross_val_score
scores = cross_val_score(model, X, y, cv=5)
Machine Question 8
Q: Plot feature importances of a decision tree.
Answer:
import matplotlib.pyplot as plt
plt.bar(range(len(model.feature_importances_)), model.feature_importances_)
Machine Question 9
Q: Normalize data using MinMaxScaler.
Answer:
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
X_scaled = scaler.fit_transform(X)
Machine Question 10
Q: Calculate accuracy, precision, recall using sklearn.
Answer:
from sklearn.metrics import accuracy_score, precision_score, recall_score
accuracy = accuracy_score(y_true, y_pred)
Machine Question 11
Q: Train a logistic regression classifier in sklearn.
Answer:
from sklearn.linear_model import LogisticRegression
model = LogisticRegression()
model.fit(X_train, y_train)
Machine Question 12
Q: Perform k-fold cross validation using sklearn.
Answer:
from sklearn.model_selection import cross_val_score
scores = cross_val_score(model, X, y, cv=5)
Machine Question 13
Q: Plot feature importances of a decision tree.
Answer:
import matplotlib.pyplot as plt
plt.bar(range(len(model.feature_importances_)), model.feature_importances_)
Machine Question 14
Q: Normalize data using MinMaxScaler.
Answer:
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
X_scaled = scaler.fit_transform(X)
Machine Question 15
Q: Calculate accuracy, precision, recall using sklearn.
Answer:
from sklearn.metrics import accuracy_score, precision_score, recall_score
accuracy = accuracy_score(y_true, y_pred)
Machine Question 16
Q: Train a logistic regression classifier in sklearn.
Answer:
from sklearn.linear_model import LogisticRegression
model = LogisticRegression()
model.fit(X_train, y_train)
Machine Question 17
Q: Perform k-fold cross validation using sklearn.
Answer:
from sklearn.model_selection import cross_val_score
scores = cross_val_score(model, X, y, cv=5)
Machine Question 18
Q: Plot feature importances of a decision tree.
Answer:
import matplotlib.pyplot as plt
plt.bar(range(len(model.feature_importances_)), model.feature_importances_)
Machine Question 19
Q: Normalize data using MinMaxScaler.
Answer:
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
X_scaled = scaler.fit_transform(X)
Machine Question 20
Q: Calculate accuracy, precision, recall using sklearn.
Answer:
from sklearn.metrics import accuracy_score, precision_score, recall_score
accuracy = accuracy_score(y_true, y_pred)