Popular Coding Questions Solution
This Repository contains all the important coding solutions, which may be asked in Mass Recruitment exams like WIPRO elite NLTH,TCS Ninja Online Tests etc and it also contains previous year solved Coding Questions.
TCS2018
Command line Program
Command line argument is a parameter supplied to the program when it is invoked.
Palindrome
A palindrome is a word, number, phrase, or other sequence of characters which reads the same backward as forward, e.g Anna,1221,Noon,Racecar etc
Number Palindrome
given an input number print its reverse as output e.g input-->123 output-->321
String Palindrome
Input: IAMGOD Output: DOGMAI
Square root of a prime number without using built in sqrt function
Perfect number
In number theory, a perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself. e.g 28 is a perfect number divisors of 28 are 1,2,4,7,14 which sum upto 28 = (1+2+4,7+14)
Factorial
In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example, The value of 0! is 1, according to the convention for an empty product.
e.g 5! = 120 i.e 5x4x3x2x1=120
Fibonnaci series
In mathematics, the Fibonacci numbers, commonly denoted Fₙ, form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is, and for n > 1.
Swap using pointers
swap the input variables
input: a=10,b=20
output:a=20,b=10
GCD of two numbers
The greatest common divisor (GCD) of two nonzero integers a and b is the greatest positive integer d such that d is a divisor of both a and b; that is, there are integers e and f such that a = de and b = df, and d is the largest such integer. The GCD of a and b is generally denoted gcd(a, b).
HCF/GCF of two numbers
HCF of two numbers is the highest common number, which is available in both the numbers. ... Sometimes, it is also called the greatest common factor (GCF) or greatest common divisor (GCD). For example, the HCF of 2 and 4 is 2, because 2 is the number which is common to both 2 and 4.
GCD of 3 nos
GCD or HCF of array
Binary to Octal
Octal to Binary
Selection sort
In computer science, selection sort is an in-place comparison sorting algorithm. It has an O(n²) time complexity, which makes it inefficient on large lists, and generally performs worse than the similar
Bubble sort
Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted.
Partial sort:1st half in ascending and 2nd half in descending
Removing vowels from string
A vowel is a syllabic speech sound pronounced without any stricture in the vocal tract. Vowels are one of the two principal classes of speech sounds, the other being the consonant. Vowels vary in quality, in loudness and also in quantity.
In english language there are 5 vowels "a, e, i, o, u"
Capitalizing string or Decaptializing
input : hello world
output: Hello World
Printing alternate prime numbers
prime: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41...
Alternate Prime: 2, 5, 11, 17, 23, 31, 41...
Matrix multiplication and its Inverse
In mathematics, particularly in linear algebra, matrix multiplication is a binary operation that produces a matrix from two matrices. For matrix multiplication, the number of columns in the first matrix must be equal to the number of rows in the second matrix. The resulting matrix, known as the matrix product, has the number of rows of the first and the number of columns of the second matrix. The product of matrices A and B is denoted as AB
Matrix Multiplication
Matrix Inverse ## Printing camel case
Linear search algorithm
In computer science, a linear search or sequential search is a method for finding an element within a list. It sequentially checks each element of the list until a match is found or the whole list has been searched.
Armstrong number:
An Armstrong number is a number such that the sum of its digits raised to the third power is equal to the number itself.
For example, 371 is an Armstrong number, since 3^3+7^3+1^3 = 371
1. Given input n print first n elements of the Series of the form 2,6,6,12,12,12,20,20,20,20,30,30,30,30,30....
Between Two Sets
There will be two arrays of integers. Determine all integers that satisfy the following two conditions:
- The elements of the first array are all factors of the integer being considered
- The integer being considered is a factor of all elements of the second array
These numbers are referred to as being between the two arrays. Determine how many such numbers exist.
Example
input:
a=[2,4]
b=[16,32,96]
output: 3
ArrayRotation
Given a vector array (A) and a rotation count(k) rotate the array K times,where rotation indicates shifting each element ofthe array 1 digit right, so after k rotationsgiven an index the program should return the element at that index (from the resulting array)
Find Majority Element
Given an array of size n, return the majority element.
The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array.
Input :- [2,3,3,2,2]
Output :- 2
## [Sorting the Sentence](https://leetcode.com/problems/sorting-the-sentence/)
A sentence is a list of words that are separated by a single space with no leading or trailing spaces. Each word consists of lowercase and uppercase English letters.
A sentence can be shuffled by appending the 1-indexed word position to each word then rearranging the words in the sentence.
For example, the sentence "This is a sentence" can be shuffled as "sentence4 a3 is2 This1" or "is2 sentence4 This1 a3". Given a shuffled sentence s containing no more than 9 words, reconstruct and return the original sentence.
Input: s = "is2 sentence4 This1 a3" Output: "This is a sentence" Explanation: Sort the words in s to their original positions "This1 is2 a3 sentence4", then remove the numbers.