Basic DSA Questions
Basic DSA Questions
Problem Description
Given a number, return the reverse of that number.
Input format
One line of input containing an integer N, the number to be reversed.
Output format
Return the reversed number. Note that the number must start with (1-9) digits only
unless it is a single digit number.
Sample Input 1
15
Sample Output 1
51
Constraints
0 <= N <= 1000000
Explanation
Reverse of 15 is 51.
Resource
Reverse Digits of a Number
Crio Methodology - Problem Approach
Brainstorm some approaches for solving the problem before viewing the video
Problem Description
Given an array of n elements and an integer x,find the index where x is present in
the array. If there are multiple occurrences, find the leftmost one. If x is not
present, return -1.
Input format
There are three lines of input.
Output format
An integer representing the index of x in the array.
Sample Input 1
5
1 3 4 2 1
Sample Output 1
0
Explanation 1
1 is present at 0 and 4 indexes and the leftmost index is 0.
Constraints
1 <= n <= 100000
3.
Problem Description
Given the heights of n students of a class, find the average height. Your answer
should be accurate upto 5 decimal places.
Input format
There are two lines of input.
Second line contains n space-separated decimal numbers - The array with the heights
of the students.
Output format
Print the average height.
Sample Input 1
6
Sample Output 1
2.033333
Explanation 1
(2.2+1+3+1.9+2.4+1.7) / 6 = 12.2/6 = 2.03333
Constraints
0 < n < 100
Resource
Array Average - Iterative and Recursive
Crio Methodology - Problem Approach
Brainstorm some approaches for solving the problem before viewing the video
Problem Description
Given a paragraph of words, capitalise the first character of each word and return
the updated paragraph.
Input format
One line of input which contains a string, the paragraph.
Output format
Return the paragraph after capitalising each word.
Sample Input 1
the quick Brown fox jumps over The lazy dog.
Sample Output 1
The Quick Brown Fox Jumps Over The Lazy Dog.
Explanation 1
The first letter of each word has been capitalised and other permitted
characters(dot ‘.’) have remained the same.
Sample Input 2
the quick Brown .... fox jumps over The lazy dog
Sample Output 2
The Quick Brown .... Fox Jumps Over The Lazy Dog.
Explanation 2
The first letter of each word has been capitalised and other permitted
characters(dot ‘.’) have remained the same.
Constraints
Length of paragraph < 100
Resource
Convert First character of each word to uppercase
Crio Methodology - Problem Approach
Brainstorm some approaches for solving the problem before viewing the video
Problem Description
Given a string, count the total number of vowels present in that string.
Note: The string contains uppercase and lowercase english alphabets only.
Input format
One line of input, which contains the given string.
Output format
Print the total number of vowels.
Sample Input 1
language
Sample Output 1
4
Explanation 1
There are a total of 4 vowels in the string "language" i.e. 'a', 'u', 'a', 'e'.
Constraints
0 < Length of string < 100
Resource
Iterative and recursive methods to count vowels in a string
Crio Methodology - Problem Approach
Brainstorm some approaches for solving the problem before viewing the video
Problem Description
Given a string, count the total number of vowels present in that string.
Note: The string contains uppercase and lowercase english alphabets only.
Input format
One line of input, which contains the given string.
Output format
Print the total number of vowels.
Sample Input 1
language
Sample Output 1
4
Explanation 1
There are a total of 4 vowels in the string "language" i.e. 'a', 'u', 'a', 'e'.
Constraints
0 < Length of string < 100
Resource
Iterative and recursive methods to count vowels in a string
Crio Methodology - Problem Approach
Brainstorm some approaches for solving the problem before viewing the video
7. Diagonal MAtrix
Problem Description
Given a matrix of dimensions n x n, find the sum of elements along the principal
diagonal of the matrix.
Principal diagonal is the diagonal that starts at top left(0,0),(1,1) and goes to
the bottom right (n-1,n-1).
Input format
There are n+1 lines of input.
Output format
An integer representing the sum of diagonal elements.
Sample Input 1
4
1 2 3 4
1 2 4 5
2 3 3 4
1 1 2 3
Sample Output 1
9
Constraints
1 <= n <= 100
Resource
Sum of diagonal Elements
Crio Methodology
Problem Description
Given a matrix of dimensions n x n having elements 1 to n*n distinct elements,
check whether the matrix is magic square or not.
Magic square is a square that has the same sum along all rows, columns and
diagonals.
Input format
There are n + 1 lines of input.
Output format
Print "Yes" if it is a magic square , "No" otherwise.
Sample Input 1
3
4 9 2
3 5 7
8 1 6
Sample Output 1
Yes
Explanation 1
All rows, columns and diagonals have sum 15.
Constraints
1 <= n <= 100
Problem Description
Given two matrices of dimension n x m, add the two matrices and print the resultant
matrix.
Input format
There are 2n+1 lines of input.
In the next 2n lines, each line contains m elements. The first n lines are for the
first matrix, next n for the second matrix.
Output format
Print the resultant matrix.
Sample Input 1
2 2
1 2
3 4
1 1
1 1
Sample Output 1
2 3
4 5
Constraints
1 <= n,m <= 100
Resource
Addition of two matrices
Description
Assessment
Problem Description
Given an array of n integers, find the maximum element in the given array.
Note : Do not use any inbuilt functions that find the maximum element directly.
Input format
There are two lines of input
Output format
Print the maximum element in the array.
Sample Input 1
5
1 2 3 1 2
Sample Output 1
3
Explanation 1
3 is the maximum value in the array.
Constraints
1 <= n <= 10000
Resource
Largest element in array
Crio Methodology - Problem Approach
Brainstorm some approaches for solving the problem before viewing the video
Problem Description
Given a number n, you have to print a triangle-shaped pattern with n rows using
space separated '*'.
image
You will have to return an array of strings, with each element of the array
representing one row of the pattern.
Input format
There is one line of input, containing an integer n, the number of rows.
Output format
Print the pattern as described.
Sample Input 1
4
Sample Output 1
image
Constraints
0 < n < 100
Resource
Printing Pyramid patterns
Input format
There are q+3 lines of input.
Second line contains n space separated integers representing the array elements.
Output format
In each query an integer representing the index of x in the array.
Sample Input 1
5
1 2 9 9 10
10
Sample Output 1
2
-1
Explanation 1
1 is present at 0 and 4 indexes and the leftmost index is 0.
3 is not present.
10 is at 4th index.
Constraints
1 <= n <= 100000
Resource
First and last positions of element in sorted array
Problem Description
Given a number N, you have to print all the odd numbers upto N in increasing order.
Input format
One line of input, containing an integer - N.
Output format
Print the numbers space-separated in a single line in increasing order.
Sample Input 1
5
Sample Output 1
1 3 5
Constraints
1 <= N <= 100
Resource
Print all odd numbers from 1 to n - C