Sei sulla pagina 1di 30

Placement Prep

Practice (P^3)
1 Combination Sum
Company Tag: ABCO, Accolite, Amazon, Infosys, Microsoft
Given an array of integers A and a sum B, find all
unique combinations in A where the sum is equal to
B.

Each number in A may only be used once in the


combination.


Note:
All numbers will be positive integers.
Elements in a combination (a1, a2, … , ak) must be in
non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
The combinations themselves must be sorted in
ascending order.
If there is no combination possible the print
"Empty" (without quotes).
Example,
Given A = 10,1,2,7,6,1,5 and B(sum) 8,

A solution set is:

“ [1, 7]
[1, 2, 5]
[2, 6]
[1, 1, 6]
Input
◇ First is T , no of test cases. 1<=T<=500
◇ Every test case has three lines.
◇ First line is N, size of array. 1<=N<=12
◇ Second line contains N space seperated integers(x).
1<=x<=9.
◇ Third line is the sum B. 1<=B<=30.
Output
◇ One line per test case, every subset enclosed in ()
and in every set intergers should be space
seperated
Example
◇ Input:
◇ 2
◇ 7
◇ 10 1 2 7 6 1 5
◇ 8
◇ 5
◇ 81868
◇ 12

◇ Output:
◇ (1 1 6)(1 2 5)(1 7)(2 6)
◇ Empty
2 Kadane’s Algorithm
Company Tag: 24*7 Innovation Labs, Accolite, Amazon, Citrix, D-E
Shaw, FactSet, Flipkart, Hike, Housing.com, MakeMyTrip, MetLife,
Microsoft, Morgan Stanley, Ola Cabs, Oracle, Oyo Rooms, Payu,
Samsung, Snapdeal, Teradata, Visa, VMware, Walmart, Zoho
Given an array arr of N integers. Find


the contiguous sub-array with
maximum sum.
Input
◇ The first line of input contains an integer T denoting
the number of test cases. The description of T test
cases follows. The first line of each test case
contains a single integer N denoting the size of
array. The second line contains N space-separated
integers A1, A2, ..., AN denoting the elements of the
array.
Output and
Constraints
◇ Print the maximum sum of the contiguous sub-array
in a separate line for each test case.
◇ 1 ≤ T ≤ 110
1 ≤ N ≤ 106
-107 ≤ A[i] <= 107
Example
◇ Example:
Input
2
5
1 2 3 -2 5
4
-1 -2 -3 -4
Output
9
-1
Sort all even numbers in
ascending order and then sort
all odd numbers in descending
3 order
Company Tag: Microsoft Interview Question
Given an array of integers (both odd
and even), sort them in such a way that


the first part of the array contains odd
numbers sorted in descending order,
rest portion contains even numbers
sorted in ascending order.
Examples:


Input : arr[] = {1, 2, 3, 5, 4, 7, 10}
Output : arr[] = {7, 5, 3, 1, 2, 4, 10}

Input : arr[] = {0, 4, 5, 3, 7, 2, 1}


Output : arr[] = {7, 5, 3, 1, 0, 2, 4}
Print a pattern without
4 using any loop
Company Tag: Microsoft Interview Question
Given a number n, print following a
pattern without using any loop.

Examples :

“ Input: n = 16
Output: 16, 11, 6, 1, -4, 1, 6, 11, 16

Input: n = 10
Output: 10, 5, 0, 5, 10
5 Count of strings
Company Tag: Google Interview Question
Count of strings that can be formed
using a, b and c under given
constraints


Given a length n, count the number of
strings of length n that can be made
using ‘a’, ‘b’ and ‘c’ with at-most one ‘b’
and two ‘c’s allowed.
Input : n = 3
Output : 19
Below strings follow given constraints:


aaa aab aac aba abc aca acb acc baa
bac bca bcc caa cab cac cba cbc cca ccb

Input : n = 4
Output : 39
5 Overlapping rectangles
Company Tag: Google Interview Question
Given two rectangles, find if the given
two rectangles overlap or not. A
rectangle is denoted by providing the x
and y co-ordinates of two points: the
left top corner and the right bottom
corner of the rectangle. Two rectangles


sharing a side are considered
overlapping.

Note : It may be assumed that the


rectangles are parallel to the coordinate
axis.
Input
◇ The first integer T denotes the number of testcases.
For every test case, there are 2 lines of input. The
first line consists of 4 integers: denoting the co-
ordinates of the 2 points of the first rectangle. The
first integer denotes the x co-ordinate and the
second integer denotes the y co-ordinate of the left
topmost corner of the first rectangle. The next two
integers are the x and y co-ordinates of right bottom
corner. Similarly, the second line denotes the
coordinates of the two points of the second
rectangle in similar fashion.
Output and
Constraints
◇ For each testcase, output (either 1 or 0) denoting
whether the 2 rectangles are overlapping. 1 denotes
the rectangles overlap whereas 0 denotes the
rectangles do not overlap.
◇ 1 <= T <= 10
-104 <= x, y <= 104
Example
◇Input:
2
0 10 10 0
5 5 15 0
0211
-2 -3 0 2
◇Output:
1
0
Sort an array in wave
6 form
Company Tag: Google , MakeMyTrip, Microsoft
Given an unsorted array of integers,
sort the array into a wave like array. An


array ‘arr[0..n-1]’ is sorted in wave form
if arr[0] >= arr[1] <= arr[2] >= arr[3] <=
arr[4] >= …..
Examples:

Input: arr[] = {10, 5, 6, 3, 2, 20, 100, 80}


Output: arr[] = {10, 5, 6, 2, 20, 3, 100, 80}
OR


{20, 5, 10, 2, 80, 6, 100, 3} OR

Input: arr[] = {20, 10, 8, 6, 4, 2}


Output: arr[] = {20, 8, 10, 4, 6, 2} OR
{10, 8, 20, 2, 6, 4} OR
Input: arr[] = {2, 4, 6, 8, 10, 20}
Output: arr[] = {4, 2, 8, 6, 20, 10} OR

“ Input: arr[] = {3, 6, 5, 10, 7, 20}


Output: arr[] = {6, 3, 10, 5, 20, 7} OR
Thanks!
Any questions?
You can find me at:
◇ ashwing2981999@gmail.com
◇ +91 97897 61211

Potrebbero piacerti anche