0% found this document useful (0 votes)
14 views

Basic DSA Questions

Uploaded by

Mukul Vasanth
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Basic DSA Questions

Uploaded by

Mukul Vasanth
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

Reverse a give String

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

2. find give an Element in array

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.

First line contains integer n.

Second line contains n space separated integers representing the array.

Third line contains the value of x.

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

0 <= x <= 10^9

Crio Methodology - Problem Approach

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.

First line contains an integer n,the number of students.

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

2.2 1 3 1.9 2.4 1.7

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

0 < height < 100

Resource
Array Average - Iterative and Recursive
Crio Methodology - Problem Approach
Brainstorm some approaches for solving the problem before viewing the video

4. Capitalise first letter of every word

Problem Description
Given a paragraph of words, capitalise the first character of each word and return
the updated paragraph.

Note : No inbuilt function such as split() to be used.

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

The paragraph contains spaces, lowercase, uppercase characters and ‘.’.

Resource
Convert First character of each word to uppercase
Crio Methodology - Problem Approach
Brainstorm some approaches for solving the problem before viewing the video

5. Count Vowels in a string

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

6. Count vowels in string

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.

First line contains integer n.

In the next n lines each line contains n elements.

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

0 <= element of matrix <= 10^5

Resource
Sum of diagonal Elements
Crio Methodology

8. Check if sum is matrix square

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.

First line contains integer n.

The next n lines contain n space separated elements.

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

1 <= element of matrix <= n x n


Crio Methodology - Problem Approach
Brainstorm some approaches for solving the problem before viewing the video

9. Addition of two matrixes

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.

First line contains two space separated integers n, m.

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

0 <= element of matrices <= 100000

Resource
Addition of two matrices

How to Create a 2D Matrix in Javascript

Crio Methodology - Problem Approach


Brainstorm some approaches for solving the problem before viewing the video

10. Check if max element in matrix

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

First line contains the integer n.

Next line contains n space separated integers.

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

1 <= element of array <= 100000

Resource
Largest element in array
Crio Methodology - Problem Approach
Brainstorm some approaches for solving the problem before viewing the video

11. Pritn Pyramid Pattern

Problem Description
Given a number n, you have to print a triangle-shaped pattern with n rows using
space separated '*'.

For eg. if n = 4 pattern will be:

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

12. Find queried element in array


Problem Description
Given a sorted array containing n elements and q queries. Each query is an integer
x. Find the index at which x is present in the array. If there are multiple
occurrences then find the leftmost one. If x is not present, return -1.

Note : No inbuilt functions such as ( upperbound , lowerbound , etc. ) to be used.

Input format
There are q+3 lines of input.

First line contains the integer n.

Second line contains n space separated integers representing the array elements.

Third line contains the value of q.

Next q line contains the integer x.

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

1 <= q <= 100000

0 <= x <= 10^9

Resource
First and last positions of element in sorted array

13. Print odd numbers upto n

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

Print all odd numbers from 1 to n - Python

Crio Methodology - Problem Approach


Brainstorm some approaches for solving the problem before viewing the video

You might also like