Practice Ques
Practice Ques
Difficulty: Easy
Topics: Basic Programming
Description: Write a program to check whether a number is even or odd.
Example:
Input: number = 4
Output: Even
Explanation: Since 4 is divisible by 2, it is an even number.
Identifying Palindromes
Difficulty: Easy
Topics: Basic Programming, String Manipulation
Description: Write a program to check if a string or number is a palindrome.
Example:
Input: string = "radar"
Output: Palindrome
Explanation: "radar" reads the same backward as forward.
*
***
*****
*******
*********
Explanation: A pyramid pattern with a height of 5 is generated.
Reversing a String
Difficulty: Easy
Topics: Basic Programming, String Manipulation
Description: Write a program to reverse a given string.
Example:
Input: string = "programming"
Output: "gnimmargorp"
Explanation: The reversed string of "programming" is "gnimmargorp".
Sorting an Array
Difficulty: Easy
Topics: Basic Programming, Sorting Algorithms
Description: Write a program to sort an array of numbers in ascending order.
Example:
Input: array = [3, 1, 4, 1, 5, 9]
Output: [1, 1, 3, 4, 5, 9]
Explanation: The array sorted in ascending order is [1, 1, 3, 4, 5, 9].
4 x 1 = 4
4 x 2 = 8
4 x 3 = 12
4 x 4 = 16
4 x 5 = 20
Explanation: The multiplication table for 4 up to 5 is generated.
Finding Prime Numbers in a Range
Difficulty: Easy
Topics: Basic Programming, Number Theory
Description: Write a program to find all prime numbers within a given range.
Example:
Input: range = [10, 30]
Output: [11, 13, 17, 19, 23, 29]
Explanation: Prime numbers between 10 and 30 are 11, 13, 17, 19, 23, and 29.
1
2 3
4 5 6
Explanation: A number pattern with 3 rows is generated.
1
1 1
1 2 1
1 3 3 1
Explanation: Pascal's Triangle with 4 rows is generated.
1 2 3
4 5 6
7 8 9
Explanation: A 3x3 matrix is generated with sequential numbers.
1
12
123
1234
Explanation: A number pyramid with 4 rows is generated.
s2
2 x 1 = 2 3 x 1 = 3 4 x 1 = 4
2 x 2 = 4 3 x 2 = 6 4 x 2 = 8
2 x 3 = 6 3 x 3 = 9 4 x 3 = 12
2 x 4 = 8 3 x 4 = 12 4 x 4 = 16
Calculating the Sum of a Series (1 + 1/2 + 1/3 + ... + 1/n)
Difficulty: Medium
Topics: Mathematical Computations
Description: Write a program to calculate the sum of the series 1 + 1/2 + 1/3 + ...
+ 1/n up to the nth term.
Example:
Input: n = 4
Output: 2.083333
Explanation: Sum of the series is 1 + 1/2 + 1/3 + 1/4 ≈ 2.083333.
*
***
*****
***
*
Counting the Number of Palindromic Substrings in a String
Difficulty: Medium
Topics: String Manipulation
Description: Write a program to count how many palindromic substrings exist in a
given string.
Example:
Input: string = "aaa"
Output: 6
Explanation: Palindromic substrings are "a", "a", "a", "aa", "aa", "aaa".
1 2 3
2 4 6
3 6 9
Finding the GCD of Multiple Numbers
Difficulty: Medium
Topics: Mathematical Computations
Description: Write a program to find the GCD (Greatest Common Divisor) of an array
of numbers.
Example:
Input: array = [12, 24, 36]
Output: 12
Explanation: The GCD of 12, 24, and 36 is 12.
1 1 2
3 5 8
13 21 34
Finding the Sum of the First N Prime Numbers
Difficulty: Medium
Topics: Prime Numbers, Mathematical Computations
Description: Write a program to calculate the sum of the first N prime numbers.
Example:
Input: N = 4
Output: 17
Explanation: The sum of the first 4 prime numbers (2 + 3 + 5 + 7) is 17.
1 2 3
8 9 4
7 6 5
Finding All Subsets of a Set
Difficulty: Medium
Topics: Combinatorics
Description: Write a program to generate all possible subsets of a given set of
numbers.
Example:
Input: set = [1, 2]
Output: [[], [1], [2], [1, 2]]
Explanation: The subsets of [1, 2] are the empty set, [1], [2], and [1, 2].
ations
Description: Write a program to check which numbers in a given range are perfect
squares.
Example:
Input: start = 1, end = 10
Output: [1, 4, 9]
Explanation: Perfect squares between 1 and 10 are 1, 4, and 9.
Finding the Sum of Diagonal Elements in a Matrix
Difficulty: Easy
Topics: Matrix Operations
Description: Write a program to calculate the sum of the diagonal elements in a
square matrix.
Example:
Input: matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Output: 15
Explanation: The sum of the diagonal elements (1 + 5 + 9) is 15.
1
1 1
1 2 1
Finding the Sum of Digits of the Product of Two Numbers
Difficulty: Easy
Topics: Mathematical Computations
Description: Write a program to find the sum of the digits of the product of two
given numbers.
Example:
Input: number1 = 12, number2 = 34
Output: 9
Explanation: The product of 12 and 34 is 408, and the sum of its digits is 4 + 0 +
8 = 12.
1
12
123
1234
Finding All Divisors of the Product of Two Numbers
Difficulty: Medium
Topics: Number Theory
Description: Write a program to find all divisors of the product of two given
numbers.
Example:
Input: number1 = 6, number2 = 10
Output: [1, 2, 3, 5, 6, 10, 15, 30]
Explanation: The product of 6 and 10 is 60, and its divisors are listed.
1 0 0 0
1 1 0 0
1 1 1 0
1 1 1 1
Finding the Sum of the First N Even Numbers
Difficulty: Easy
Topics: Mathematical Computations
Description: Write a program to calculate the sum of the first N even numbers.
Example:
Input: N = 4
Output: 20
Explanation: The first 4 even numbers are 2, 4, 6, 8, and their sum is 20.
2
2 3
2 3 5
Finding the Common Elements in Two Arrays
Difficulty: Medium
Topics: Arrays
Description: Write a program to find common elements between two arrays.
Example:
Input: array1 = [1, 2, 3, 4], array2 = [3, 4, 5, 6]
Output: [3, 4]
Explanation: The common elements between the two arrays are 3 and 4.
1
12
123
Finding the Largest Element in Each Row of a Matrix
Difficulty: Easy
Topics: Matrix Operations
Description: Write a program to find the largest element in each row of a matrix.
Example:
Input: matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Output: [3, 6, 9]
Explanation: The largest elements in each row are 3, 6, and 9.
3 8 1
7 4 6
Finding the Length of the Longest Word in a String
Difficulty: Easy
Topics: String Manipulation
Description: Write a program to find the length of the longest word in a given
string.
Example:
Input: string = "Find the longest word"
Output: 8
Explanation: The longest word is "longest" with length 8.
7 3 5
2 6 9
1 8 4
Finding the Difference Between the Sum of Even and Odd Numbers in an Array
Difficulty: Easy
Topics: Arrays, Mathematical Computations
Description: Write a program to calculate the difference between the sum of even
and odd numbers in an array.
Example:
Input: array = [1, 2, 3, 4, 5, 6]
Output: 4
Explanation: The sum of even numbers is 12, and the sum of odd numbers is 8. The
difference is 4.
*
**
***
****
s3
Example 1: Input: n = 4
Output:
*
**
***
****
Problem 2: Print a Square of Stars
Difficulty: Easy
Topics: Pattern Printing
Hint: Print a square pattern of stars (*). Each row and column should have the same
number of stars.
Example 1: Input: n = 5
Output:
*****
*****
*****
*****
*****
Problem 3: Print a Pyramid Pattern
Difficulty: Easy
Topics: Pattern Printing
Hint: Print a pyramid pattern with stars (*). The pyramid should have a single peak
and each row should have an increasing number of stars, centered horizontally.
Example 1: Input: n = 3
Output:
*
***
*****
Problem 4: Print a Diamond Pattern
Difficulty: Easy
Topics: Pattern Printing
Hint: Print a diamond pattern with stars (*). The pattern should include a single
peak in the middle with symmetric rows above and below it.
Example 1: Input: n = 3
Output:
*
***
*****
***
*
Problem 5: Print a Hollow Square of Stars
Difficulty: Easy
Topics: Pattern Printing
Hint: Print a hollow square pattern with stars (*). The border of the square should
be filled with stars while the inner part should be empty.
Example 1: Input: n = 5
Output:
*****
* *
* *
* *
*****
Problem 6: Print a Number Triangle
Difficulty: Easy
Topics: Pattern Printing
Hint: Print a right-angle triangle pattern with numbers. Each row should contain an
increasing sequence of numbers starting from 1.
Example 1: Input: n = 4
Output:
1
12
123
1234
Problem 7: Print an Inverted Triangle Pattern
Difficulty: Easy
Topics: Pattern Printing
Hint: Print an inverted triangle pattern with stars (*). Each row should contain
decreasing numbers of stars from the top row.
Example 1: Input: n = 5
Output:
*****
****
***
**
*
Problem 8: Print a Diamond Pattern with Numbers
Difficulty: Easy
Topics: Pattern Printing
Hint: Print a diamond pattern with numbers. The pattern should have a peak in the
middle with symmetric rows above and below it.
Example 1: Input: n = 3
Output:
1
121
12321
121
1
Problem 9: Print a Right-Angle Triangle of Numbers
Difficulty: Easy
Topics: Pattern Printing
Hint: Print a right-angle triangle pattern with increasing numbers. Each row should
contain a continuous sequence of increasing numbers.
Example 1: Input: n = 4
Output:
1
23
456
78910
Problem 10: Print a Pyramid Pattern with Numbers
Difficulty: Easy
Topics: Pattern Printing
Hint: Print a pyramid pattern with increasing numbers. Each row should have an
increasing sequence of numbers, centered horizontally.
Example 1: Input: n = 3
Output:
1
232
34543
Problem 11: Print a Pattern of Alternating 0s and 1s
Difficulty: Medium
Topics: Matrix Pattern
Hint: Print a matrix where elements alternate between 0 and 1. The pattern should
alternate both row-wise and column-wise.
Example 1: Input: n = 4
Output:
0101
1010
0101
1010
Problem 12: Print a Pascal’s Triangle
Difficulty: Medium
Topics: Matrix Pattern
Hint: Print Pascal’s Triangle up to N rows. Each row should be constructed based on
the sum of the elements directly above it in the previous row.
Example 1: Input: n = 4
Output:
1
1 1
1 2 1
1 3 3 1
Problem 13: Print a Pattern of Consecutive Numbers
Difficulty: Medium
Topics: Matrix Pattern
Hint: Print a matrix of consecutive numbers starting from 1, filling rows
sequentially.
Example 1: Input: n = 3
Output:
1 2 3
4 5 6
7 8 9
Problem 14: Print a Star Pattern with Increasing Width
Difficulty: Medium
Topics: Pattern Printing
Hint: Print a pattern where each row has an increasing width of stars.
Example 1: Input: n = 3
Output:
*
***
*****
Problem 15: Print a Right-Angle Triangle Pattern with Characters
Difficulty: Medium
Topics: Pattern Printing
Hint: Print a right-angle triangle pattern using characters. Each row should
contain the same character repeated according to the row number.
Example 1: Input: n = 3
Output:
A
BB
CCC
Problem 16: Print a Checkerboard Pattern
Difficulty: Medium
Topics: Matrix Pattern
Hint: Print a checkerboard pattern with two different characters alternating.
Example 1: Input: n = 4
Output:
XOXOXO
OXOXOX
XOXOXO
OXOXOX
Problem 17: Print a Pyramid Pattern of Increasing Stars
Difficulty: Medium
Topics: Pattern Printing
Hint: Print a pyramid pattern where each row increases in the number of stars.
Example 1: Input: n = 3
Output:
*
***
*****
Problem 18: Print a Border Pattern with Numbers
Difficulty: Medium
Topics: Matrix Pattern
Hint: Print a border pattern using numbers. The border should be filled with
numbers, and the inner part should be empty.
Example 1: Input: n = 4
Output:
1234
1 1
1 1
1234
Problem 19: Print an Inverted Pyramid Pattern with Characters
Difficulty: Medium
Topics: Pattern Printing
Hint: Print an inverted pyramid pattern using characters. Each row should have
decreasing characters from the top row.
Example 1: Input: n = 3
Output:
CCC
BB
A
Problem 20: Print a Cross Pattern with Stars
Difficulty: Medium
Topics: Pattern Printing
Hint: Print a cross pattern using stars. The cross should be centered within a
matrix.
Example 1: Input: n = 5
Output:
***
*
*
*
***
Problem 21: Print a Spiral Matrix
Difficulty: Hard
Topics: Matrix Pattern
Hint: Print a matrix filled with numbers in a spiral pattern. The numbers should
start from 1 and increment as you move around the spiral.
Example 1: Input: n = 3
Output:
1 2 3
8 9 4
7 6 5
Problem 22: Print a Diamond Pattern with Increasing Width
Difficulty: Hard
Topics: Pattern Printing
Hint: Print a diamond pattern where each line has increasing width of stars.
Example 1: Input: n = 3
Output:
*
***
*****
***
*
Problem 23: Print a Diamond Pattern with Numbers Increasing
Difficulty: Hard
Topics: Pattern Printing
Hint: Print a diamond pattern where numbers increase. Each row should show a
symmetrical pattern with numbers increasing towards the center.
Example 1: Input: n = 3
Output:
1
121
12321
121
1
Problem 24: Print a Pattern of Increasing and Decreasing Stars
Difficulty: Hard
Topics: Pattern Printing
Hint: Print a pattern where stars increase to a midpoint and then decrease.
Example 1: Input: n = 3
Output:
*
***
*****
***
*
Problem 25: Print a Matrix with Zigzag Pattern
Difficulty: Hard
Topics: Matrix Pattern
Hint: Print a matrix with a zigzag pattern of numbers. The numbers should alternate
direction row-wise.
Example 1: Input: n = 3
Output:
1 2 3 4
8 7 6 5
9 10 11 12
Problem 26: Print a Pattern of Alternating Characters in Rows
Difficulty: Hard
Topics: Pattern Printing
Hint: Print a pattern where rows alternate between two characters.
Example 1: Input: n = 4
Output:
ABAB
BABA
ABAB
BABA
Problem 27: Print a Number Pyramid Pattern with Characters
Difficulty: Hard
Topics: Pattern Printing
Hint: Print a pyramid pattern using increasing characters, where each row increases
in width and character range.
Example 1: Input: n = 3
Output:
A
BCD
EFGHI
Problem 28: Print a Pattern with Diagonal Lines
Difficulty: Hard
Topics: Pattern Printing
Hint: Print a pattern with diagonal lines using characters. Each diagonal line
should be aligned properly.
Example 1: Input: n = 4
Output:
A
B B
C C
D D
Problem 29: Print a Matrix with Diamond Pattern of Numbers
Difficulty: Hard
Topics: Matrix Pattern
Hint: Print a matrix where elements follow a diamond pattern with numbers.
Example 1: Input: n = 3
Output:
1
121
12321
121
1
Problem 30: Print a Cross Pattern of Stars with Diagonals
Difficulty: Hard
Topics: Pattern Printing
Hint: Print a cross pattern using stars with intersecting diagonals.
Example 1: Input: n = 5
Output:
* * * * *
* * * *
* * *
* *
*
* *
* * *
* * * *
* * * * *
Problem 31: Print a Triangular Matrix with Numbers
Difficulty: Easy
Topics: Pattern Printing
Hint: Print a triangular matrix where each row contains increasing numbers. Each
subsequent row should start from the next number.
Example 1: Input: n = 3
Output:
1
2 3
4 5 6
Problem 32: Print a Star Pattern with Increasing and Decreasing Width
Difficulty: Medium
Topics: Pattern Printing
Hint: Print a pattern with stars that increase to a midpoint and then decrease. The
stars should be centered horizontally.
Example 1: Input: n = 3
Output:
*
***
*****
***
*
Problem 33: Print a Pattern of Nested Squares
Difficulty: Medium
Topics: Pattern Printing
Hint: Print a pattern with nested squares using stars. The outermost square should
be filled with stars, and each subsequent square should be smaller and centered
inside the previous one.
Example 1: Input: n = 5
Output:
*****
* *
* *
* *
*****
Problem 34: Print a Pattern with Increasing Characters in Columns
Difficulty: Medium
Topics: Pattern Printing
Hint: Print a pattern where each column contains increasing characters from A.
Example 1: Input: n = 3
Output:
A
B C
D E F
Problem 35: Print a Matrix with Spiral Diagonals
Difficulty: Hard
Topics: Matrix Pattern
Hint: Print a matrix with numbers arranged in diagonal spirals. The numbers should
fill the matrix in a diagonal spiral fashion.
Example 1: Input: n = 3
Output:
1 2 3
4 5 6
7 8 9
Problem 36: Print a Checkerboard Pattern with Increasing Size
Difficulty: Medium
Topics: Pattern Printing
Hint: Print a checkerboard pattern where the size of each square increases as you
move along the matrix.
Example 1: Input: n = 3
Output:
XOX
OXO
XOX
Problem 37: Print a Cross Pattern with Increasing Size
Difficulty: Medium
Topics: Pattern Printing
Hint: Print a cross pattern where the size of the cross increases with each row.
The pattern should be centered.
Example 1: Input: n = 3
Output:
*
***
*****
***
*
Problem 38: Print a Pattern of Alternating Triangles
Difficulty: Medium
Topics: Pattern Printing
Hint: Print a pattern with alternating triangles of stars. The triangles should
alternate direction.
Example 1: Input: n = 3
Output:
*
***
*****
***
*
Problem 39: Print a Matrix with Diamond Pattern of Numbers
Difficulty: Medium
Topics: Pattern Printing
Hint: Print a matrix where numbers form a diamond pattern. The numbers should
increase and decrease symmetrically around the center.
Example 1: Input: n = 3
Output:
1
121
12321
121
1
Problem 40: Print a Star Pattern with Increasing Width and Centered
Difficulty: Medium
Topics: Pattern Printing
Hint: Print a pattern where the width of stars increases, and the stars are
centered horizontally.
Example 1: Input: n = 3
Output:
*
***
*****
Problem 41: Print a Pattern with Spiral and Zigzag
Difficulty: Hard
Topics: Matrix Pattern
Hint: Print a matrix with both spiral and zigzag patterns. The matrix should first
fill in a spiral pattern and then in a zigzag fashion.
Example 1: Input: n = 3
Output:
1 2 3
6 5 4
7 8 9
Problem 42: Print a Pattern of Alternating Characters in Matrix
Difficulty: Medium
Topics: Pattern Printing
Hint: Print a matrix where characters alternate in each cell to form a pattern.
Example 1: Input: n = 3
Output:
ABAB
BABA
ABAB
Problem 43: Print a Pattern with Nested Triangles
Difficulty: Medium
Topics: Pattern Printing
Hint: Print a pattern with nested triangles of stars. Each triangle should be
centered and decrease in size.
Example 1: Input: n = 3
Output:
*
***
*****
***
*
Problem 44: Print a Matrix with Increasing Rows and Columns
Difficulty: Easy
Topics: Matrix Pattern
Hint: Print a matrix where each row and column contains increasing numbers.
Example 1: Input: n = 3
Output:
1 2 3
4 5 6
7 8 9
Problem 45: Print a Pattern with Rows of Increasing Characters
Difficulty: Medium
Topics: Pattern Printing
Hint: Print a pattern where each row contains an increasing sequence of characters.
Example 1: Input: n = 3
Output:
A
BC
DEF
Problem 46: Print a Star Pattern with Diamond Shape and Numbers
Difficulty: Medium
Topics: Pattern Printing
Hint: Print a pattern with a diamond shape of stars, with numbers inside the
diamond.
Example 1: Input: n = 3
Output:
1
121
12321
121
1
Problem 47: Print a Matrix with Cross Pattern of Numbers
Difficulty: Hard
Topics: Matrix Pattern
Hint: Print a matrix where the center forms a cross pattern with numbers.
Example 1: Input: n = 5
Output:
12321
01210
01210
01210
12321
Problem 48: Print a Pattern with Concentric Squares
Difficulty: Medium
Topics: Pattern Printing
Hint: Print a pattern with concentric squares using stars. The outer square should
be larger and each subsequent square should be centered inside.
Example 1: Input: n = 5
Output:
*****
* *
* *
* *
*****
Problem 49: Print a Pattern of Alternating Rows and Columns of Numbers
Difficulty: Easy
Topics: Pattern Printing
Hint: Print a pattern with alternating rows and columns of numbers, where each row
and column increases sequentially.
Example 1: Input: n = 3
Output:
123
456
789
Problem 50: Print a Matrix with Zigzag Pattern of Stars
Difficulty: Medium
Topics: Pattern Printing
Hint: Print a matrix where stars form a zigzag pattern, alternating rows in their
positioning.
Example 1: Input: n = 3
Output:
* * *
* *
* * *