Practice Questions for Programming in Cpp
Practice Questions for Programming in Cpp
Practice Questions:
1. You are given a 2D array (matrix) of size m × n. Print the elements of the array in a wave-like order:
⎡ 1 2 3 4 ⎤
⎢ 5 6 7 8 ⎥
⎣ 9 10 11 12 ⎦
Output:
1 5 9 10 6 2 3 7 11 12 8 4
2. You are given a square matrix of size n × n (where number of rows = number of columns). Your task is
to calculate and print its determinant.
3. Given a matrix with 0s and 1s, if any cell is 1, make all cells in that row and column 1.
4. Given a matrix, multiply it by its transpose and print the result.
5. Write code to print the frequency of each character in a given sequence.
6. Write C/C++ code to check whether a matrix is unique (a matrix with all unique elements must be
considered unique for this question).
7. Write C/C++ code to calculate the sum of all matrix elements (excluding the center) and store the sum in
the center of the matrix.
8. Rotate Matrix by 90 Degrees. Given an N x N matrix, rotate it clockwise by 90° in-place.
9. Spiral Order Traversal. Print a 2D array in spiral order.
⎡ 1 2 3 ⎤
⎢ 4 5 6 ⎥
⎣ 7 8 9 ⎦
Output:
1 23698745
10. Find Saddle Point. A saddle point is an element which is the minimum in its row and maximum in its
column. Print it if exists.
11. Write a C program which takes a character array as input and reverses it in place. (without using an extra
array)
12. Write a program that finds consecutive character patterns and prints all patterns on screen.
Input: Taaaghncfffahig
Output: aaa
fff
13. Write a program that prints all unique characters in a string. After printing, sorts them in ascending order
and prints them again.
14. Write a program that takes a character array as input (including spaces) and prints the length of last
word.
15. Write a C or C++ program that takes a string as input and reverses the order of the words, but does not
reverse the characters within each word.
Input: I love Coding
Output: Coding love I
16. Given a character array, remove duplicate characters without using any extra space (modify in place).
Input: ['a','b','a','c','b']
Output: ['a','b','c']
17. Given two character arrays, check if one is a rotation of the other.
Input: ['a','b','c','d'], ['c','d','a','b']
Output: YES
18. Given a character array, find the character that appears the most times.
19. Given a character array, move all spaces to the end without using another array.
Input: ['h','e',' ','l','l',' ','o']
Output: ['h','e','l','l','o',' ',' ']
20. Given a square matrix, find the sum of its main diagonal elements.
21. Given a character array, compress it by replacing sequences of the same character with the character
followed by the count.
Input: ['a','a','b','b','c','c','c']
Output: ['a','2','b','2','c','3'], Length: 6
22. Given a character array, find the length of the longest palindromic substring.
Input: ['b','a','n','a','n','a']
Output: 5
23. Given a character array, find the index of the first non-repeating character. Print -1 if all characters
repeat.
Input: ['a','b','a','c','c','d']
Output: 1
24. Given a binary matrix where each row is sorted, find the index of the row with the maximum number of
1s.
Input:
⎡ 0 0 1 1 ⎤
⎡ 0 1 1 1 ⎤
⎢ 0 0 0 1 ⎥
Output: 1
25. Given a m x n matrix where each row and column is sorted in ascending order, search for a key. Row
and column indices if found, otherwise print 'Not Found'.
26. Replace every character in a character array with its previous character in the alphabet (e.g., b → a, a →
z).
27. Print only the boundary elements of a 2D matrix clock-wise i.e. top row → last column → bottom row
→ first column.
28. Given a character array with spaces (e.g., ['t','h','e',' ','s','k','y',' ','i','s',' ','b','l','u','e']), reverse the order of
words in place without extra space.