Practice twenty-eight core coding problems formatted like standard coding platforms: problem statement, constraints, examples, and test cases.
You are given distance (in kilometers) and time (in minutes). Convert time into hours and calculate the speed in km/h.
Speed = Distance / Time (in hours)
Input:
Distance = 30
Time = 30
Output:
60
Explanation:
Time in hours = 30 / 60 = 0.5
Speed = 30 / 0.5 = 60
| Distance | Time | Output |
|---|---|---|
| 30 | 30 | 60 |
| 100 | 50 | 120 |
| 10 | 0 | Error |
| 45 | 70 | Error |
Given an integer array (can contain both positive and negative numbers), find the maximum subarray sum and the subarray itself. Use Kadane's algorithm.
Maximum Subarray: <sum>, Subarray: [elements]
Input:
arr = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
Output:
Maximum Subarray: 6, Subarray: [4, -1, 2, 1]
| Input | Output |
|---|---|
| [-2, 1, -3, 4, -1, 2, 1, -5, 4] | Max Sum: 6, Subarray: [4, -1, 2, 1] |
| [1, 2, 3, 4] | Max Sum: 10, Subarray: [1, 2, 3, 4] |
| [-1, -2, -3] | Max Sum: -1, Subarray: [-1] |
You are given a 2D matrix representing marks of students. Each row represents a student, and each column represents marks in exams.
You are also given an index (i, j). Calculate the total marks (internal + external) of the student at row i.
Note: The student is identified by row i. The marks are taken from column 0 and column 1.
Input:
2 2
40 50
30 60
1 1
Output:
90
Explanation:
Student at index (1,1) -> row = 1
Internal = 30, External = 60
Total = 90
You are given efficiencies of n people. You need to form groups of size t. If some people are left (n is not divisible by t), those extra people are removed.
For each group, find the difference between the highest and lowest efficiency. Return the maximum difference among all groups.
Input:
6
1 3 4 9 10 12
2
Output:
5
Explanation:
Sorted array: [1, 3, 4, 9, 10, 12]
Groups: (1,3) -> 2, (4,9) -> 5, (10,12) -> 2
Maximum difference = 5
Given an array of elements, remove all duplicate elements while preserving the order of their first occurrence. After removing duplicates, print the resulting array in reverse order.
Input:
7
1 2 3 2 4 1 5
Output:
5 4 3 2 1
Explanation:
Original array: 1 2 3 2 4 1 5
After removing duplicates: 1 2 3 4 5
Reverse the array: 5 4 3 2 1
A sorted array is rotated k times in a clockwise direction. Given this rotated array of size N and a target element K:
If the element is not present, return -1.
Input:
7
4 5 6 7 1 2 3
2
Output:
4
1
Explanation:
Original sorted array: 1 2 3 4 5 6 7
Rotated array: 4 5 6 7 1 2 3
Number of rotations = index of minimum element = 4
Element 2 is at index 1 in original sorted array
Given an array of integers, count the frequency of each element and return the element that has the highest frequency. If multiple elements have the same highest frequency, return the smallest element among them.
Input:
6
1 3 2 3 4 1
Output:
1
Explanation:
Frequency: 1 -> 2, 3 -> 2, 2 -> 1, 4 -> 1
Both 1 and 3 have same frequency (2), smallest is 1.
You are given an integer n, an array cost[] of size n, and an n x n symmetric matrix where diagonal elements are 0. For each row, replace the diagonal element (0) with cost[i] and compute the sum of that row. Return the minimum row sum among all rows.
Input:
3
5 6 7
0 1 3
1 0 2
3 2 0
Output:
9
Explanation:
Row 0 -> replace 0 with 5 -> [5,1,3] -> sum = 9
Row 1 -> replace 0 with 6 -> [1,6,2] -> sum = 9
Row 2 -> replace 0 with 7 -> [3,2,7] -> sum = 12
Minimum = 9
You are given an array of integers. Implement a modified Quick Sort partition step to determine the pivot index after placing the pivot element in its correct sorted position. The pivot is chosen as the last element of the array.
Return the final index of the pivot.
n
a1 a2 a3 ... an
pivot_index
Input:
5
4 2 7 1 3
Process:
Pivot = 3
After partition -> [2, 1, 3, 4, 7]
Output:
2
You are given a graph with n nodes and m edges. Each edge has a weight. Find the minimum cost to connect all nodes using Kruskal's algorithm. If it is not possible to connect all nodes, return -1.
n m
u1 v1 w1
u2 v2 w2
...
um vm wm
minimum_cost
Input:
4 5
0 1 10
0 2 6
0 3 5
1 3 15
2 3 4
Output:
19
Explanation:
Edges chosen:
(2,3) -> 4
(0,3) -> 5
(0,1) -> 10
Total = 19
You are given a purchase amount. Based on the amount, a discount is applied as follows:
Calculate the final payable amount after applying the discount.
Test Case 1:
Input:
800
Output:
760.00
Test Case 2:
Input:
2000
Output:
1800.00
Test Case 3:
Input:
6000
Output:
5100.00
A king wants to arrange his army in a line following strict rules. You are given:
Rules:
Find the total number of valid arrangements of length N.
Test Case 1:
Input:
4 4 3
Output:
7
Test Case 2:
Input:
3 3 2
Output:
2
A parking system calculates fines based on the number of hours a vehicle is parked. Given an integer hours representing parking duration, calculate the fine:
Input: 2
Output: 100
Input: 4
Output: 50
Input: 6
Output: 20
You are given an array of integers of size n and an integer maxSum. Find the maximum possible sum of elements from the array such that the sum is less than or equal to maxSum. You can choose any subset of elements.
Test Case 1
Input:
n = 4
arr = [2, 3, 5, 7]
maxSum = 10
Output:
10
Explanation: subset {3, 7} gives sum 10.
Test Case 2
Input:
n = 3
arr = [4, 8, 6]
maxSum = 9
Output:
8
Explanation: best subset is {8}.
Given an integer array nums, find the contiguous subarray (containing at least one number) that has the largest product, and return that product.
Input: [2, 3, -2, 4]
Output: 6
Input: [-2, 0, -1]
Output: 0
Input: [-2, -3, -4]
Output: 12
Given two arrays s and x of size n containing the same elements (possibly in different order), you can perform adjacent swaps on array s. Find the minimum number of adjacent swaps required to transform s into x. If it is not possible, return -1.
Input:
n = 5
s = [20, 40, 29, 10, 98]
x = [10, 98, 40, 20, 29]
Output:
7
A movie theatre has ticket prices stored in an array of size N. Find all odd ticket prices and compute the sum, count, and average of odd prices.
Input:
4
20 25 30 35
Output:
60 2 30.00
You are given BreadCost[] and FillingCost[] arrays and an integer Target. You need to make a sandwich such that total cost is closest to Target.
Input:
BreadCost = [5, 7]
FillingCost = [2, 3]
Target = 10
Output:
10
You are given 10 predefined ranks in ascending order: 1 2 3 4 5 6 7 8 9 10. Take an integer N as input.
Input:
3
Output:
3 8
Input:
11
Output:
Invalid Input
You are given N (number of boxes), W (maximum total weight allowed in one group), and N integers representing weights of boxes. Boxes must be taken in order.
Rules:
Print both values.
N W
W1 W2 W3 ... WN
number_of_groups boxes_used
Input:
6 10
2 3 4 5 1 2
Output:
2 6
Input:
5 7
2 3 5 4 2
Output:
2 3
You are given N pairs of integers (a, b). Sort the pairs using Selection Sort based on these rules:
Input:
5
10 4
3 2
5 2
3 1
10 5
Output:
3 1
3 2
5 2
10 4
10 5
You are given an undirected weighted graph. Find the Minimum Spanning Tree (MST) and the Second Minimum Spanning Tree. The second MST must be strictly greater than the MST and minimum among all such spanning trees.
Input:
4 5
1 2 1
1 3 2
2 3 1
2 4 3
3 4 4
Output:
5 6
You are given a 3 x 3 matrix. Determine whether the matrix forms a trapezoid pattern.
If the matrix is a trapezoid, print the sum of the first row elements. Otherwise, print the product of the first row elements.
3 x 3 matrix elements
Sum or Product of first row
Input:
1 2 3
4 5 0
6 0 0
Output:
6
Input:
1 2 3
4 5 6
7 8 9
Output:
6
A medical store maintains a digital database of medicines with unique ID and stock level.
Count medicines by stock level and print whether reorder is required.
Input:
5
101 2
102 1
103 0
104 2
105 1
Output:
Full: 2
Half: 2
Out: 1
Reorder Required
A gym offers membership plans based on months:
Print "Invalid Input" or "Cost: <amount>".
Input:
1
Output:
Cost: 2000
Input:
3
Output:
Cost: 5000
Input:
5
Output:
Cost: 9000
Input:
7
Output:
Cost: 15000
Input:
0
Output:
Invalid Input
The system processes N transactions. Each transaction has sender, receiver, timestamp, and amount.
Rules:
Test Case 1
Input:
3
A B 10 100
C D 50 200
E F 100 300
Output:
All Transactions Valid
Test Case 2
Input:
3
A B 10 100
C D 50 200
A B 70 300
Output:
Error: Duplicate Transaction
Test Case 3
Input:
3
A B 10 100
C D 50 200
E F 200 300
Output:
Fraud Detected
You are given an integer N (minimum charge required) and an array of integers representing the charge in each laptop. Count how many laptops have charge >= N.
N
A1 A2 A3 A4 ... An
Single integer -> number of laptops that can work
Input:
5
2 3 6 7 1
Output:
2
You are given an integer N, representing the number of people initially in the Happy state. At each iteration:
Simulate this process for 4 iterations and print final Happy and Sad counts.
A single integer N
Final_Happy Final_Sad