Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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.
“
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,
“ [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}
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.
“
array ‘arr[0..n-1]’ is sorted in wave form
if arr[0] >= arr[1] <= arr[2] >= arr[3] <=
arr[4] >= …..
Examples:
“
{20, 5, 10, 2, 80, 6, 100, 3} OR