Python Practice Questions

Topic-based coding problems with sample test cases — build confidence step by step.

List Practice

Python lists — ordered, mutable collections perfect for sequential data.

1
Sum of Elements in a List
Easy
Write a program to find the sum of all elements in a given list of integers.
First line contains integer N (1 ≤ N ≤ 10⁵). Second line contains N space-separated integers.
Print a single integer — the sum of all elements.
Sample Input 1
5
1 2 3 4 5
Sample Output 1
15
Sample Input 2
3
-5 0 3
Sample Output 2
-2
Sample Input 3
4
10 -10 20 -20
Sample Output 3
0
Sample Input 4
1
100
Sample Output 4
100
1 ≤ N ≤ 10⁵, -10⁶ ≤ arr[i] ≤ 10⁶
2
Access List Elements by Index
Easy
Write a program that takes a list and an index, and prints the element at that index. If the index is out of range, print "Index out of range". Indices are 0-based.
First line contains integer N. Second line contains N space-separated integers. Third line contains integer K (index to access).
Print the element at index K, or "Index out of range".
Sample Input 1
5
10 20 30 40 50
2
Sample Output 1
30
Sample Input 2
3
100 200 300
5
Sample Output 2
Index out of range
Sample Input 3
1
99
0
Sample Output 3
99
Sample Input 4
4
1 2 3 4
-1
Sample Output 4
Index out of range
0 ≤ N ≤ 10⁵, 0 ≤ K < 10⁵
3
Count Occurrences in a List
Easy
Write a program to count how many times a given element X appears in a list.
First line contains integer N. Second line contains N space-separated integers. Third line contains integer X.
Print a single integer — the count of X in the list.
Sample Input 1
8
3 1 4 1 5 1 9 2
1
Sample Output 1
3
Sample Input 2
5
7 8 9 10 11
5
Sample Output 2
0
Sample Input 3
6
5 5 5 5 5 5
5
Sample Output 3
6
Sample Input 4
4
-1 -2 -3 -1
-1
Sample Output 4
2
1 ≤ N ≤ 10⁵, -10⁶ ≤ arr[i], X ≤ 10⁶
4
Append Elements to a List
Easy
Write a program that reads N integers and builds a list by appending each element. Print the final list.
First line contains integer N. Second line contains N space-separated integers.
Print the list as comma-separated values inside square brackets, e.g. [1, 2, 3].
Sample Input 1
4
7 8 9 10
Sample Output 1
[7, 8, 9, 10]
Sample Input 2
1
42
Sample Output 2
[42]
Sample Input 3
5
-5 0 3 -1 2
Sample Output 3
[-5, 0, 3, -1, 2]
Sample Input 4
3
100 200 300
Sample Output 4
[100, 200, 300]
1 ≤ N ≤ 100
5
Find Minimum and Maximum in a List
Easy
Write a program to find the smallest and largest element in a given list of integers.
First line contains integer N. Second line contains N space-separated integers.
Print two integers separated by a space: min and max.
Sample Input 1
6
45 12 78 3 99 23
Sample Output 1
3 99
Sample Input 2
3
-7 15 0
Sample Output 2
-7 15
Sample Input 3
1
5
Sample Output 3
5 5
Sample Input 4
4
-50 -20 -100 -10
Sample Output 4
-100 -10
1 ≤ N ≤ 10⁵, -10⁹ ≤ arr[i] ≤ 10⁹
6
Reverse a List
Easy
Write a program to reverse a given list and print the reversed list.
First line contains integer N. Second line contains N space-separated integers.
Print the reversed list as space-separated integers.
Sample Input 1
5
1 2 3 4 5
Sample Output 1
5 4 3 2 1
Sample Input 2
1
42
Sample Output 2
42
Sample Input 3
4
0 -1 -2 -3
Sample Output 3
-3 -2 -1 0
Sample Input 4
6
5 6 7 8 9 10
Sample Output 4
10 9 8 7 6 5
1 ≤ N ≤ 10⁴
7
Check if Element Exists in List
Easy
Write a program to check if a given element X exists in the list. Print "Found" or "Not Found".
First line contains integer N. Second line contains N space-separated integers. Third line contains X.
Print "Found" if X exists, otherwise "Not Found".
Sample Input 1
6
10 20 30 40 50 60
35
Sample Output 1
Not Found
Sample Input 2
6
10 20 30 40 50 60
30
Sample Output 2
Found
Sample Input 3
1
7
7
Sample Output 3
Found
Sample Input 4
4
1 2 3 4
100
Sample Output 4
Not Found
1 ≤ N ≤ 10⁵, -10⁶ ≤ arr[i], X ≤ 10⁶
8
Remove Duplicates from a List
Easy
Write a program to remove duplicate elements from a list while preserving the original order of first occurrences.
First line contains integer N. Second line contains N space-separated integers.
Print the list with duplicates removed, space-separated.
Sample Input 1
8
4 2 4 3 2 1 3 5
Sample Output 1
4 2 3 1 5
Sample Input 2
5
1 2 3 4 5
Sample Output 2
1 2 3 4 5
Sample Input 3
5
10 10 10 10 10
Sample Output 3
10
Sample Input 4
6
-1 0 -1 1 2 0
Sample Output 4
-1 0 1 2
1 ≤ N ≤ 10⁴, -10⁶ ≤ arr[i] ≤ 10⁶
9
Sort a List in Ascending Order
Easy
Write a program to sort a given list of integers in ascending order.
First line contains integer N. Second line contains N space-separated integers.
Print the sorted list as space-separated integers.
Sample Input 1
7
9 3 7 1 5 2 8
Sample Output 1
1 2 3 5 7 8 9
Sample Input 2
4
10 20 30 40
Sample Output 2
10 20 30 40
Sample Input 3
5
3 -5 8 -10 0
Sample Output 3
-10 -5 0 3 8
Sample Input 4
3
2 1 1
Sample Output 4
1 1 2
1 ≤ N ≤ 10⁵, -10⁹ ≤ arr[i] ≤ 10⁹
10
Find the Length of a List
Easy
Write a program to find and print the number of elements in a given list.
First line contains integer N. Second line contains N space-separated integers.
Print a single integer — the length of the list.
Sample Input 1
5
100 200 300 400 500
Sample Output 1
5
Sample Input 2
1
7
Sample Output 2
1
Sample Input 3
8
1 2 3 4 5 6 7 8
Sample Output 3
8
Sample Input 4
3
-10 0 15
Sample Output 4
3
1 ≤ N ≤ 10⁵
1
Second Largest Element
Medium
Write a program to find the second largest element in a list of distinct integers.
First line contains integer N (N ≥ 2). Second line contains N space-separated distinct integers.
Print the second largest element.
Sample Input 1
6
10 5 8 20 3 15
Sample Output 1
15
Sample Input 2
3
100 -5 50
Sample Output 2
50
Sample Input 3
2
5 10
Sample Output 3
5
Sample Input 4
7
99 12 45 23 78 42 33
Sample Output 4
78
2 ≤ N ≤ 10⁵, -10⁹ ≤ arr[i] ≤ 10⁹
2
Rotate a List by K Positions
Medium
Write a program to rotate a list to the right by K positions. Elements at the end wrap around to the beginning.
First line contains integer N. Second line contains N space-separated integers. Third line contains integer K (0 ≤ K ≤ 10⁹).
Print the rotated list as space-separated integers.
Sample Input 1
7
1 2 3 4 5 6 7
3
Sample Output 1
5 6 7 1 2 3 4
Sample Input 2
5
1 2 3 4 5
0
Sample Output 2
1 2 3 4 5
Sample Input 3
5
1 2 3 4 5
5
Sample Output 3
1 2 3 4 5
Sample Input 4
4
1 2 3 4
2
Sample Output 4
3 4 1 2
1 ≤ N ≤ 10⁵, 0 ≤ K ≤ 10⁹
3
Merge Two Sorted Lists
Medium
Write a program to merge two sorted lists into a single sorted list.
First line contains integer N. Second line contains N sorted integers. Third line contains M. Fourth line contains M sorted integers.
Print the merged sorted list as space-separated integers.
Sample Input 1
4
1 3 5 7
4
2 4 6 8
Sample Output 1
1 2 3 4 5 6 7 8
Sample Input 2
3
1 2 3
3
4 5 6
Sample Output 2
1 2 3 4 5 6
Sample Input 3
3
1 2 3
3
1 4 5
Sample Output 3
1 1 2 3 4 5
Sample Input 4
2
-5 3
2
0 10
Sample Output 4
-5 0 3 10
1 ≤ N, M ≤ 10⁵
4
Most Frequent Element
Medium
Write a program to find the element that appears most frequently in a list. If tied, print the smallest such element.
First line contains integer N. Second line contains N space-separated integers.
Print the most frequent element (smallest in case of tie).
Sample Input 1
9
3 1 4 1 5 3 1 9 3
Sample Output 1
1
Sample Input 2
7
5 5 5 2 2 2 2
Sample Output 2
2
Sample Input 3
5
3 3 3 3 3
Sample Output 3
3
Sample Input 4
8
-1 -1 0 0 1 1 2 2
Sample Output 4
-1
1 ≤ N ≤ 10⁵, -10⁶ ≤ arr[i] ≤ 10⁶
5
Remove Elements at Even Indices
Medium
Write a program to remove all elements at even indices (0-based) from a list and print the remaining elements.
First line contains integer N. Second line contains N space-separated integers.
Print the elements at odd indices as space-separated values. If none, print nothing.
Sample Input 1
6
10 20 30 40 50 60
Sample Output 1
20 40 60
Sample Input 2
1
42
Sample Output 2

                      
Sample Input 3
5
1 2 3 4 5
Sample Output 3
2 4
Sample Input 4
4
a b c d
Sample Output 4
b d
1 ≤ N ≤ 10⁴
6
Intersection of Two Lists
Medium
Write a program to find the intersection of two lists (common elements without duplicates). Return in order of first appearance in the first list.
First line contains integer N. Second line contains N space-separated integers. Third line contains M. Fourth line contains M space-separated integers.
Print common elements space-separated. If none, print nothing.
Sample Input 1
6
1 2 3 4 5 6
5
4 5 6 7 8
Sample Output 1
4 5 6
Sample Input 2
4
1 2 3 4
4
5 6 7 8
Sample Output 2

                      
Sample Input 3
4
1 2 1 2
3
2 1 3
Sample Output 3
1 2
Sample Input 4
5
0 5 10 15 20
4
0 10 100 200
Sample Output 4
0 10
1 ≤ N, M ≤ 10⁵, -10⁶ ≤ arr[i] ≤ 10⁶
7
Check if List is Palindrome
Medium
Write a program to check if a list reads the same forwards and backwards.
First line contains integer N. Second line contains N space-separated integers.
Print "Palindrome" or "Not Palindrome".
Sample Input 1
5
1 2 3 2 1
Sample Output 1
Palindrome
Sample Input 2
4
1 2 3 4
Sample Output 2
Not Palindrome
Sample Input 3
1
7
Sample Output 3
Palindrome
Sample Input 4
6
1 2 2 3 3 1
Sample Output 4
Not Palindrome
1 ≤ N ≤ 10⁵
8
Flatten a Nested List
Medium
Write a program to flatten a list of lists (2D) into a single 1D list in row-major order.
First line contains integer R (rows). Next R lines: C followed by C integers.
Print the flattened list as space-separated integers.
Sample Input 1
3
3 1 2 3
2 4 5
4 6 7 8 9
Sample Output 1
1 2 3 4 5 6 7 8 9
Sample Input 2
2
3 10 20 30
1 40
Sample Output 2
10 20 30 40
Sample Input 3
1
1 5
Sample Output 3
5
Sample Input 4
4
1 1
1 2
0
1 3
Sample Output 4
1 2 3
1 ≤ R, C ≤ 100
9
Move Zeros to End
Medium
Write a program to move all zeros in a list to the end while preserving the order of non-zero elements.
First line contains integer N. Second line contains N space-separated integers.
Print the modified list as space-separated integers.
Sample Input 1
8
0 1 0 3 12 0 5 0
Sample Output 1
1 3 12 5 0 0 0 0
Sample Input 2
4
1 2 3 4
Sample Output 2
1 2 3 4
Sample Input 3
5
0 0 0 0 0
Sample Output 3
0 0 0 0 0
Sample Input 4
6
5 0 5 0 5 0
Sample Output 4
5 5 5 0 0 0
1 ≤ N ≤ 10⁵, -10⁶ ≤ arr[i] ≤ 10⁶
10
Split List into Chunks
Medium
Write a program to split a list into chunks of size K. The last chunk may be smaller.
First line contains integer N. Second line contains N space-separated integers. Third line contains K (chunk size).
Print each chunk on a new line as space-separated integers.
Sample Input 1
8
1 2 3 4 5 6 7 8
3
Sample Output 1
1 2 3
4 5 6
7 8
Sample Input 2
4
1 2 3 4
1
Sample Output 2
1
2
3
4
Sample Input 3
6
1 2 3 4 5 6
6
Sample Output 3
1 2 3 4 5 6
Sample Input 4
5
10 20 30 40 50
2
Sample Output 4
10 20
30 40
50
1 ≤ N ≤ 10⁴, 1 ≤ K ≤ N
1
Longest Consecutive Subsequence
Hard
Write a program to find the length of the longest consecutive elements sequence in an unsorted list. Must run in O(N) time.
First line contains integer N. Second line contains N space-separated integers.
Print a single integer — the length of the longest consecutive sequence.
Sample Input 1
9
100 4 200 1 3 2 5 6 7
Sample Output 1
5
Sample Input 2
5
10 20 30 40 50
Sample Output 2
1
Sample Input 3
7
1 1 2 2 3 3 5
Sample Output 3
3
Sample Input 4
1
42
Sample Output 4
1
1 ≤ N ≤ 10⁵, -10⁹ ≤ arr[i] ≤ 10⁹
2
Find All Triplets That Sum to Zero
Hard
Write a program to find all unique triplets in a list that sum to zero. Triplets should be sorted internally, no duplicates.
First line contains integer N. Second line contains N space-separated integers.
Print each triplet on a new line (ascending). If none, print "No triplets found".
Sample Input 1
6
-1 0 1 2 -1 -4
Sample Output 1
-1 -1 2
-1 0 1
Sample Input 2
3
1 2 3
Sample Output 2
No triplets found
Sample Input 3
4
0 0 0 0
Sample Output 3
0 0 0
Sample Input 4
5
-2 1 1 0 2
Sample Output 4
-2 0 2
-2 1 1
3 ≤ N ≤ 3000, -10⁵ ≤ arr[i] ≤ 10⁵
3
Maximum Subarray Sum (Kadane's Algorithm)
Hard
Write a program to find the maximum sum of any contiguous subarray using Kadane's algorithm.
First line contains integer N. Second line contains N space-separated integers.
Print a single integer — the maximum subarray sum.
Sample Input 1
9
-2 1 -3 4 -1 2 1 -5 4
Sample Output 1
6
Sample Input 2
5
1 2 3 4 5
Sample Output 2
15
Sample Input 3
5
-1 -2 -3 -4 -5
Sample Output 3
-1
Sample Input 4
1
10
Sample Output 4
10
1 ≤ N ≤ 10⁵, -10⁴ ≤ arr[i] ≤ 10⁴
4
Product of Array Except Self
Hard
Write a program to compute an array where each element is the product of all elements except itself — without division, O(N) time.
First line contains integer N. Second line contains N space-separated integers.
Print N space-separated integers.
Sample Input 1
5
1 2 3 4 5
Sample Output 1
120 60 40 30 24
Sample Input 2
3
0 1 2
Sample Output 2
0 0 0
Sample Input 3
4
-1 2 -3 1
Sample Output 3
-6 3 -2 6
Sample Input 4
2
1 2
Sample Output 4
2 1
2 ≤ N ≤ 10⁵, -10⁶ ≤ arr[i] ≤ 10⁶
5
Find the Missing Number (1 to N)
Hard
Write a program to find the missing number from N-1 distinct integers from 1 to N.
First line contains N. Second line contains N-1 distinct integers (one missing).
Print the missing number.
Sample Input 1
6
1 2 4 5 6
Sample Output 1
3
Sample Input 2
5
1 2 3 4
Sample Output 2
5
Sample Input 3
5
2 3 4 5
Sample Output 3
1
Sample Input 4
2
1
Sample Output 4
2
2 ≤ N ≤ 10⁵
6
Majority Element II
Hard
Write a program to find all elements that appear more than ⌊N/3⌋ times.
First line contains integer N. Second line contains N space-separated integers.
Print majority elements ascending. If none, print "None".
Sample Input 1
8
3 2 3 1 2 3 2 3
Sample Output 1
2 3
Sample Input 2
5
1 1 1 2 3
Sample Output 2
1
Sample Input 3
5
1 2 3 4 5
Sample Output 3
None
Sample Input 4
5
1 1 2 2 3
Sample Output 4
None
1 ≤ N ≤ 10⁵, -10⁹ ≤ arr[i] ≤ 10⁹
7
Subarray with Given Sum
Hard
Write a program to find a contiguous subarray that sums to target S. Return start and end indices (0-based).
First line contains N and S. Second line contains N non-negative integers.
Print start and end (inclusive). If not found, print "-1 -1".
Sample Input 1
5 12
1 2 3 7 5
Sample Output 1
1 3
Sample Input 2
4 15
5 3 2 5
Sample Output 2
0 3
Sample Input 3
3 100
1 2 3
Sample Output 3
-1 -1
Sample Input 4
6 0
1 2 0 3 4 5
Sample Output 4
2 2
1 ≤ N ≤ 10⁵, 0 ≤ arr[i] ≤ 10⁹, 0 ≤ S ≤ 10⁹
8
Rearrange Array Alternately
Hard
Write a program to rearrange a sorted list alternately: max, min, second max, second min, etc.
First line contains integer N. Second line contains N sorted integers.
Print the rearranged list as space-separated integers.
Sample Input 1
6
1 2 3 4 5 6
Sample Output 1
6 1 5 2 4 3
Sample Input 2
5
1 2 3 4 5
Sample Output 2
5 1 4 2 3
Sample Input 3
4
1 2 3 4
Sample Output 3
4 1 3 2
Sample Input 4
3
-5 0 10
Sample Output 4
10 -5 0
1 ≤ N ≤ 10⁵, -10⁶ ≤ arr[i] ≤ 10⁶
9
Sort 0s, 1s, and 2s (Dutch National Flag)
Hard
Write a program to sort a list of 0s, 1s, 2s in O(N) time and O(1) space.
First line contains N. Second line contains N integers ∈ {0, 1, 2}.
Print the sorted list.
Sample Input 1
10
2 0 2 1 1 0 1 2 0 1
Sample Output 1
0 0 0 1 1 1 1 2 2 2
Sample Input 2
5
0 0 0 0 0
Sample Output 2
0 0 0 0 0
Sample Input 3
4
2 1 2 1
Sample Output 3
1 1 2 2
Sample Input 4
3
2 1 0
Sample Output 4
0 1 2
1 ≤ N ≤ 10⁵
10
Find Pair with Maximum Product
Hard
Write a program to find a pair of elements (distinct indices) whose product is maximum.
First line contains integer N. Second line contains N space-separated integers.
Print the pair sorted ascending.
Sample Input 1
6
-10 -3 5 6 -20 2
Sample Output 1
-20 -10
Sample Input 2
5
1 2 3 4 5
Sample Output 2
4 5
Sample Input 3
3
-5 -2 -1
Sample Output 3
-5 -2
Sample Input 4
2
7 3
Sample Output 4
3 7
2 ≤ N ≤ 10⁵, -10⁹ ≤ arr[i] ≤ 10⁹