Sei sulla pagina 1di 17

Q1. C Program to check if two given matrices are identical.

*********************************************************************************

Q2. Print a given matrix in spiral form

Given a 2D array, print it in spiral form. See the following examples.


Please comment down the code in other languages as well below –

Input:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Output:
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
Input:
1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18
Output:
1 2 3 4 5 6 12 18 17 16 15 14 13 7 8 9 10 11

******************************************************************************
Q3. Given an n-by-n matrix of 0’s and 1’s where all 1’s in each row come before all 0’s, find the
most efficient way to return the row with the maximum number of 0’s.
Input
{1,1,1,1},
{1,1,0,0},
{1,0,0,0},
{1,1,0,0},

Output
1000
****************************************************************************
Q4. A Pythagorean triplet is a set of three integers a, b and c such that a 2 + b2 = c2. Given a limit,
generate all Pythagorean Triples with values smaller than given limit.

Input : limit = 20
Output : 3 4 5
8 6 10
5 12 13
15 8 17
12 16 20

A Simple Solution is to generate these triplets smaller than given limit using three nested loop.
For every triplet, check if Pythagorean condition is true, if true, then print the triplet. Time
complexity of this solution is O(limit3) where ‘limit’ is given limit.
An Efficient Solution can print all triplets in O(k) time where k is number of triplets printed. The
idea is to use square sum relation of Pythagorean triplet, i.e., addition of squares of a and b is
equal to square of c, we can write these number in terms of m and n such that,

a = m2 - n 2
b=2*m*n
c = m2 + n 2
because,
a2 = m4 + n4 – 2 * m2 * n2
b2 = 4 * m2 * n2
c2 = m4 + n4 + 2* m2 * n2

We can see that a2 + b2 = c2, so instead of iterating for a, b and c we can iterate for m and n and
can generate these triplets.
***************************************************************************
Q5. Find GCD of two Integers?
*************************************************************************
Q6. Remove all Vowels from a String using Pointers concept?
***********************************************************************
Q7. How to Find LCM of numbers in array.
***********************************************************************
Q8. Find fibonicci series number in an array.
******************************************************************************************************
Q9. Check whether the below program print the below pattern
1111
222
33
****************************************************************************************************
Q10. Fix the error, recompile and match against the output provided.
int main(void)
{
printf(“This is a \”buggy” program\n”);
return 0;
}
***************************************************************************************************
Q11. Code reuse: Convert Binary to Decimal by using the existing function.
void binarytodecimal(number)
{
// Type your code here
}
void main()
{
int num;
scanf(“%d”, &num);
printf(“%d”, binarytodecimal(num);
}
*********************************************************************************************************
Q12. Print the prime numbers from an array up to given value n by using existing function.

**************************************************************************
Q13 To print the trapezium pattern?

1*2*3*4*17*18*19*20
5*6*7*14*15*16
8*9*12*13
10*11

**************************************************************************************
Q14. Print the following Pattern and get the output to match test cases?
To print the pattern like
for n=3
the program should print
1112
3222
3334
************************************************************************************
Q15. Programming Pattern to Print 2*N Number of rows for input Pattern?
3
44
555
6666
555
44
3
****************************************************************************
Q16. Write a program to return a sorted array from two unsorted array?
Examples:

Input : a[] = {10, 5, 15}


b[] = {20, 3, 2}
Output : Merge List :
{2, 3, 5, 10, 15, 20}

Input : a[] = {1, 10, 5, 15}


b[] = {20, 0, 2}
Output : Merge List :
{0, 1, 2, 5, 10, 15, 20}

**********************************************************************************

Q17 Find GCD of two Integers? if Numbers are both Positive and Negative?
****************************************************************************************************************
Q18 Write a program that accepts a input n and calculates the factorial of the number using
recursion
*************************************************************************************************************
Q19 Check given number is perfect number or not. Perfect number is a number which is
equal to sum of its divisor. For eg,divisors of 6 are 1,2 and 3. The sum of these divisors is 6.
So 6 is called as perfect number

********************************************************************************

Q 20 Write a program to find the sum of contiguous sub array within a one-dimensional array of
numbers which has the largest sum.

***************************************************************************

Q21 Write a program for bubble sorting


************************************************************************************************
Q22 Find index of 0 to replaced with 1 to get maximum sequence of continuous 1's
********************************************************************************

Q23 check if two Strings are anagrams of each other. Two strings are anagrams if they are
written using the same exact letters, ignoring space, punctuation, and capitalization. Each
letter should have the same count in both strings. For example, the Army and Mary are an
anagram of each other.
**************************************************************************************************************
Q24 How to program to print first non repeated character from String?

*************************************************************************************************************

Q25 Write a program to calculate the sum of an entered digit.

public class Solution{

public static int sumOfDigits(int n)

//write your code here

********************************************************************************************

Q26

Task

Given an integer, , perform the following conditional actions:

 If is odd, print Weird

 If is even and in the inclusive range of 2 to 5 , print Not Weird

 If is even and in the inclusive range of 6 to 20, print Weird

 If is even and greater than 20, print Not Weird

Complete the stub code provided in your editor to print whether or not is weird.

Input Format

A single line containing a positive integer, n.

Constraints
 1<=n<=100

Output Format

Print Weird if the number is weird; otherwise, print Not Weird.

Sample Input 0

3
Sample Output 0

Weird
Sample Input 1

24
Sample Output 1

Not Weird
Explanation

Sample Case 0: n=3

n is odd and odd numbers are weird, so we print Weird.

Sample Case 1: n=24

n >20 and is even, so it isn't weird. Thus, we print Not Weird.

*********************************************************************************************

Q27 In the magical land of Byteland, there are three kinds of citizens:

 a Bit - 2ms2ms after a Bit appears, it grows up and becomes a Nibble (i.e. it disappears and a Nibble appears)
 a Nibble - 8ms8ms after a Nibble appears, it grows up and becomes a Byte
 a Byte - 16ms16ms after a Byte appears, it grows up, splits into two Bits and disappears
Chef wants to know the answer to the following question: what would the population of Byteland be immediately
before the time NmsNms if only 1 Bit appeared at time 0ms0ms?
Help him and find the population (number of citizens) of each type.

Input

 The first line of the input contains a single integer TT denoting the number of test cases. The description
of TT test cases follows.
 The first and only line of each test case contains a single integer NN.

Output
For each test case, print a single line containing three space-separated integers — the number of Bits, Nibbles and
Bytes.

Constraints

 1≤T≤1041≤T≤104
 1≤N≤1,6001≤N≤1,600

Subtasks

Subtask #1 (25 points): 1≤N≤1401≤N≤140


Subtask #2 (75 points): original constraints

Example Input

Example Output

100

010

Explanation

Immediately before the time 2ms2ms, there is only one Bit. At 2ms2ms, this Bit grows up, so immediately
before 3ms3ms, there is only one Nibble in Byteland.
************************************************************************************

Q28 Given two strings of lowercase English letters, A and B, perform the following operations:

1. Sum the lengths of A and B .

2. Determine if A is lexicographically larger than B (i.e.: does come before in the dictionary?).

3. Capitalize the first letter in A and B and print them on a single line, separated by a space.

Input Format

The first line contains a string A. The second line contains another string B . The strings are comprised of only

lowercase English letters.


Output Format

There are three lines of output:

For the first line, sum the lengths of A and B .

For the second line, write Yes if A is lexicographically greater than B otherwise print No instead.

For the third line, capitalize the first letter in both A and B and print them on a single line, separated by a

space.

Sample Input 0

hello
java
Sample Output 0

9
No
Hello Java
Explanation 0

String A is "hello" and B is "java".

A has a length of 5, and has a length of 4 ; the sum of their lengths is 9.

When sorted alphabetically/lexicographically, "hello" precedes "java"; therefore, A is not greater than B and

the answer is No.

When you capitalize the first letter of both A and B and then print them separated by a space, you get "Hello

Java".

**********************************************************************************

Q 29
Two strings, a and b, are called anagrams if they contain all the same characters in the same frequencies. For

example, the anagrams of CAT are CAT, ACT, TAC, TCA, ATC, and CTA.

Complete the function in the editor. If a and b are case-insensitive anagrams, print "Anagrams"; otherwise,

print "Not Anagrams" instead.


Input Format

The first line contains a string denoting a.

The second line contains a string denoting b.

Constraints

 1<=length(a), length(b)<=50

 Strings a and b consist of English alphabetic characters.

 The comparison should NOT be case sensitive.

Output Format

Print "Anagrams" if a and b are case-insensitive anagrams of each other; otherwise, print "Not Anagrams"

instead.

Sample Input 0

anagram
margana
Sample Output 0

Anagrams
Explanation 0

Character Frequency: anagram Frequency: margana


A or a 3 3
G or g 1 1
N or n 1 1
M or m 1 1
R or r 1 1
The two strings contain all the same letters in the same frequencies, so we print "Anagrams".

Sample Input 1
anagramm
marganaa
Sample Output 1

Not Anagrams
Explanation 1

Character Frequency: anagramm Frequency: marganaa


A or a 3 4
G or g 1 1
N or n 1 1
M or m 2 1
R or r 1 1
The two strings don't contain the same number of a's and m's, so we print "Not Anagrams".

Sample Input 2

Hello
hello
Sample Output 2

Anagrams
Explanation 2

Character Frequency: Hello Frequency: hello


E or e 1 1
H or h 1 1
L or l 2 2
O or o 1 1
The two strings contain all the same letters in the same frequencies, so we print "Anagrams".

************************************************************************************
Q30:

Task

Given the meal price (base cost of a meal), tip percent (the percentage of the meal price being added as
tip), and tax percent (the percentage of the meal price being added as tax) for a meal, find and print the
meal's total cost.

Note: Be sure to use precise values for your calculations, or you may end up with an incorrectly rounded
result!

Input Format

There are lines of numeric input:

The first line has a double, (the cost of the meal before tax and tip).

The second line has an integer, (the percentage of being added as tip).

The third line has an integer, (the percentage of being added as tax).

Output Format

Print the total meal cost, where is the rounded integer result of the entire bill ( with added tax and tip).

Sample Input

12.00

20

Sample Output

15

***********************************************************************************

Q31

Task

Given an integer, , print its first multiples. Each multiple (where ) should be printed on a new line in the
form: n x i = result.

Input Format

A single integer, .

Constraints

Output Format
Print lines of output; each line (where ) contains the of in the form:

n x i = result.

*******************************************************************************

Q 32:

The religious act which Barney and his friends hold most sacred, XORING the natural numbers in the
given range. This time Barney is a bit busy with picking up some girls, so he asked you to help him. He
gave you two numbers L and R, you have to find if XOR of all the numbers in range L to R (L,R both
inclusive) is odd or even.

Warning!! Large Input-Output. Please use Fast IO.

Input:

The first line will contain T, number of testcases.

Each testcase contains a single line of input, two integers L,R.

Output:

For each testcase, in the new line print "Odd" if the XOR in the range is odd, else print "Even".

Constraints

1≤T≤106

1≤L≤R≤1018

Sample Input:

14

26

33

23

Sample Output:

Even

Even

Odd
Odd

EXPLANATION:

Test case -1 -> XOR (1,2,3,4) =4

Test case -2 -> XOR (2,3,4,5,6) =6

Test case -3 -> XOR (3) =3

Test case -4 -> XOR (2,3) =1

*********************************************************************************

Q33:
Write a program to print following pattern.

If n=3

12*

1*

public class Solution{

public static void pattern(int n)

//write your code here

**********************************************************************************
Q34 WAP to add a node in linkedlist
Q35 Reverse a string using stack
Q36 Print reverse linkedlist
Q37 Create a linkedlist which contains name and sort them.
Q38 WAP to take 10 values in queue and print it reversely using stack.

Q39 Write Kruskal’s algorithm for finding shortest path in a graph.

Q40 WAP to add two singly linkedlist using stack.


Q41. WAP to create a structure using BST.
Q42. WAP for traversal (inorder, postorder, inorder) of BST.
Q43 Write a function to count number of nodes in a tree
Q44 Write a function to delete a node in BST
************************************************************************************************
Q45. Find index of a particular key in linked list
Int findIndex(int size, int *list, int key){

}
*************************************************************************************************
Q46 WAP to find out a key in BST using recursion
Int searchKey(node *root, int key){
}
**********************************************************************************************

Q47
Write a program for automatic generation of a password as follow:
Input: Name(First name & Lastname separated by space)
Pin(6 digit pin)
Output: the first alphabet of longer name+ full smaller name+ 6 digit pin+ length of name
in case first name and last name are of equal length
the first alphabet of first name+ full last name+ 6 digit pin+ length of name
Example 1:
Input:
Amit sharma
124001
Output:
SAmit12400113
Example 2:
Input:
Kumar Dilip
301210
Output:
KDilip30121010Q48

*************************************************************************************************

Q48
Write a Program with help of a function in any programming language for printing sum of Even
index position element and Odd index elements that satisfy all the test cases.
Input : arr = {1, 2, 3, 4, 5, 6}
Output :Even index sum:9
Odd index sum:12
Explanation: Here, n = 6 so there will be 3 even
index positions and 3 odd index positions in an array
Even = 1 + 3 + 5 = 9
Odd = 2 + 4 + 6 = 12

Input : arr = {10, 20, 30, 40, 50, 60, 70}


Output : Even index sum:160
Odd index sum:170
Explanation: Here, n = 7 so there will be 3 odd
index positions and 4 even index positions in an array
Even = 10 + 30 + 50 + 70 = 160
Odd = 20 + 40 + 60 = 120

*******************************************************************************************************
Q49 Program to convert a decimal number to base 17 number

Q50 WAP to convert decimal to binary

Q51 WAP to implement quick sort

Q52 Remove duplicate from sorted linkedlist

Q53 Rearrange a Linked List by Separating Odd Nodes from the Even Ones.

Q54 Find length of a linked list using recursion

Q55. consider the given series: 1, 2, 1, 3, 2, 5, 3, 7, 5, 11, 8, 13, 13, 17, …

This series is a mixture of 2 series – all the odd terms in this series form a Fibonacci series and
all the even terms are the prime numbers in ascending order. Now write a program to find the
Nth term in this series.

Q56 onsider the given input and output:


Input: Get 3 strings in 3 lines as input

Hello
Hi
Good Morning

Output:

 In the 1st string, replace the vowels with “


 In the 2nd string, replace the consonants with *
 In the third string, convert the lowercase letters to upper case.

Q57 What should the program below print?

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

void myfunc(char** param){

++param;

int main(){

char* string = (char*)malloc(64);

strcpy(string, “hello_World”);

myfunc(&string);
myfunc(&string);

printf(“%s\n”, string);

// ignore memory leak for sake of quiz

return 0;

Q58. Take some elements in array and store all prime numbers in linkedlist.

Q59. Reverse a string using recursion.

Q60. How do you find the middle element of a singly linked list in one pass?

Potrebbero piacerti anche