## Arrays Interview Questions

- 0of 0 votes
Given an array of n strings, sentences, where each sentences, consists of most w space-separated words, we want to perform q queries given by an array of q strings named queries where each queries consists of at most k space-separated words. To answer query queriesj, we find all the indices in sentences that contain every word in queries and assemble them into an array of indices listed in ascending order. For example, if all the words in queries are only found in sentences4, sentences9 and sentences2 , then the answer to queriesj is the array[2,4,9].

Input Format:

The first line contains an integer, n, denoting the number of Strings in sentences

Each line i of the n subsequent lines contains at most w space-separated words describing sentences

The next line contains an integer, q, denoting the number of strings in queries

Each line j of the q subsequent lines contains at most k space-separated words describing queries.

1) Sample I/P format:

3

jim likes mary

kate likes tom

tom does not like jim

2

jim tom

likes

O/P:

2

0 1

2) Sample I/P Format:

4

how it was done

are you how

it goes to it

goes done are it

2

done it

it

O/P:

0 3

0 2 2 3

Note: Solution in Java only

static void searchQueries(String[] sentences, String[] queries) {

}

- 0of 0 votes
Given the below input and ouput.

Input :

String[] input = {"hello", "world"};

output: (Higher count should come before and natural order)

hello : l=2, e=1,h=1,o=1

world: d=1,l=1,o=1,r=1,w=1

- 0of 0 votes
Given the below input and output and asked to write in Java.

Example 1)

input : {1,2,3,4, &, 12,13,14,15}

output : {15,14,13,12,1,2,3,4}

Example 2 )

input : {33,34,&,55,63}

output : {63,55,33,34}

Assumption : '&' will always be in the middle.

- 0of 0 votes
It was asked in Chargebee off campus interview. Needed solution for this problem in java

Given a string say s and k denotes the number of commas and the output should be like when you insert the comma in the string at different places and find the maximum number.

Test case 1

say s = 999 and k = 1 so the choice would be 9,99 or 99,9 in either case the maximum number is 99

Test case 2

say s=999 and k =2 so the choice will be like 9,9,9 so output will be 9

Test case 3

say s = 857 and k = 1 the choice would be 85,7 or 8,57 so the output will be like 85

- 0of 0 votes
The value obtained in the function is given back to main by using ________ keyword?

A. return B. Static C. new D. Volatile

- 0of 0 votes
You have a non empty binary array with value 0 and 1. You can flip either 0 or 1 bit of array to make the consecutive element same.You have to return the count of consecutive number with same digit.

Input : [ 1,0,1,0,0,0]

Output : 4

if you flip the value of 1st index to 1, you have 2 consecutive 1 and 2 consecutive 0 so total 4.

input : [0,0,0,0]

output : 3

input : [0]

output 1

there is bug in below code which i couldn't find it.`class Solution { int solution(int[] A) { int n = A.length; int result = 0; for (int i = 0; i < n - 1; i++) { if (A[i] == A[i + 1]) result = result + 1; } int r = 0; for (int i = 0; i < n; i++) { int count = 0; if (i > 0) { if (A[i - 1] != A[i]) count = count + 1; else count = count - 1; } if (i < n - 1) { if (A[i + 1] != A[i]) count = count + 1; else count = count - 1; } r = Math.max(r, count); } return result + r; } }`

- 0of 0 votes
N different couple go to cinema with 2N different seats. They take their place randomly. You could make swap operations. Write a code for given input what is the minimum number of swap operations for sitting all couples with their partners? Additionally, be sure that no one swaps more than 2 times.

- 1of 1 vote
Given an array of sorted integers and find the closest value to the given number. Array may contain duplicate values and negative numbers.

Example : Array : 2,5,6,7,8,8,9

Target number : 5

Output : 5

Target number : 11

Output : 9

Target Number : 4

Output : 5

- 0of 0 votes
How to find three largest numbers in array?

- 0of 0 votes
Write a logic to print the elements of 2D matrix in a spiral way?

for eg if int[][] matrix = {{1,2,3,4}{5.6,7,8}{9, 10, 11,12}};

The output should be 1 2 3 4 8 12 11 10 9 5 6 7

The interviewer asked me to implement a recursive algorithm.

- 0of 0 votes
You have a long string that contains of 0's and 1's, seprated with some delimiter (like "!"). the number of delimeters after each 0 or 1 is between 1 to 3.

for example a string such as 0! 1!!! 0!! 1!! 0! 1! 0!!! 1!! 0!! (and so on...)

Take this long string and convert it to a 7 digits number by some porgramming algorythem,

(you can use every lanugage you want)

- 1of 3 votes

- 0of 0 votes
Can multiple threads improve the time complexity to merge k sorted arrays? If so, how?

- 1of 1 vote
Write a function that takes an array of positive and negative integers and a number. This function should return true if the array contains a contiguous sub array that sums up to the number (2nd input).

He wanted an O(n) algorithm.

- 0of 0 votes
You are given an array of nodes where each node consists of node name, isValid flag, and parent Node index. so, this array actually represents a tree(forest). where root node has -1 as its index for the parent node. rest all node have their parent's index value.

You will be given this array and an index. You have to cut down the subtree from the index. Cutting down a tree means, making all the nodes of that subtree false(Isvalid flag).

He was expecting O(N) solution.

- 1of 1 vote
Q 1. You are given an array of integers, contains positive, negative and zeros. You have to written subarray whose sum is maximum in this array.

Desired Complexity is O(N) + O(1)

- -1of 1 vote
You are given an array of integers and a number K. You have to find the any continue sub-array whose elements sum is K. Please note that, the array may have positive, negative, and zeros as its element.

The desired complexity is O(N).

Example:

Input: [7 0 9 -10 0 789], K = 0

Output: Array from index 1 to Index 1.

Input: [1 2 3 5 -10] K = 0

Output: Array from Index 1 to Index 4.

If K = -2, Output would have been SubArray from Index 2 to Index 4.

- 0of 0 votes
You are given an integer array. You have to return/print an array where kth element of this array is the multiplications of all the elements from 0 to k-1 and from k+1 to n-1.

Example

input: [1 2 5 6]

output: [60 30 12 10]

- 0of 0 votes
You are given a rotated sorted array of size N. You have to search a given number into it.

Example: [4,6,8,14,90,-9,-2,0,3], Search -2.

- 0of 0 votes
Write a program which takes input a sorted array and positive number and updates the array so that if x appears m times in array then it appears exactly min(2,m) times in array. the update should be performed in one pass with no additional memory

- 1of 1 vote
You are given a vector of integers. You have to delete the odd numbers from it.

Expected complexity is O(N) Time and O(1) space

- 1of 3 votes
Given array of length n, having element 0 to n-1.

you are allowed to swap adjacent element only if Absolute difference of two element is equal to 1.

Is it possible to sort array.

If yes print sorted output.

- 0of 0 votes
Given an array of unique numbers. Find the number of pairs that make up the difference. This must be solved in under O(N^2)

`function getPairs(int[] array, int k){ HashMap<Integer,Integer> values = new HashMap<Integer,Integer>(); for(int i = 0; i < array.length; i++){ if(!values.containsKey(array[i])){ values.put(array[i],1); } } int pairs = 0; for(int i = 0; i < array.length; i++){ int diff = array[i] - k; if(values.containsKey(diff)){ pairs++; } } return pairs; }`

This will give O(N) time. O(N) Space

- 0of 0 votes
One question containing multiple questions

1) Define the structure of a function which takes an array of size n as input and returns True or False.

2) Write a function which takes an array as input and returns a string containing all the elements separated by a comma.

Ex : [0, -45, 9, 10] => "0,-45,9,10";

3) Write a function which takes two arrays ass input, and returns minimum common element in them.

Ex : [0, -90, 45, 10, 4], [4, 8, 90, 45] => 4

4) Now let's say, the function takes an array of arrays, and each array is sorted. now, returns their first common element.

Ex : [0, -90, 45, 10, 4], [4, 8, 90, 45], [-1, -3, -5, -7, 10, 4], [24, 35, 78, -90, 56, 4] => 4

- -2of 4 votes
Find the frequency of a number in array in less than bigo n time

Array 1,2,2,3,4,5,5,5,2

Input 5

Output 3

Array 1,1,1,1,

Input 1

Output 4

Keep in mind less than bigo n

Thanks

- -5of 5 votes
Fill the arrray with elements from 0 to 9.

based on thier frequency.

a[1]=3 means, 1 is repeated for 3 times(1 must present 3 times in that array)

a[2]=4 means 2 is repeated for 4 times.(2 must present twice in that array)

- 0of 0 votes
what is the best way to pass multi dimensional arrays to a function in c/c++.(dont say pointers is best, write the best syntax)

How will you read and write a matrix, with row and column number using STL.

- 0of 0 votes
Amezon_interview_3rd round:

Fill the arrray with elements from 0 to 9.

based on thier frequency.

a[1]=3 means, 1 is repeated for 3 times(1 must present 3 times in that array)

a[2]=4 means 2 is repeated for 4 times.(2 must present twice in that array)

- 0of 0 votes
You are given a string S consisting of N brackets, opening "("

and/or closing ")". The goal is to split S into two parts (left and

right), such that the number of opening brackets in the left part is

equal to the number of closing brackets in the right part.

For example, given S = "(())", the function should return 2,

because:

the first two characters of S, "((", contain two

opening brackets, and

the remaining two characters of S, "))", contain

two closing brackets.

In other example, given S = "(())))(", the function should return

4, because:

the first four characters of S, "(())", contain two

opening brackets, and

the remaining three characters of S, "))(", contain

two closing brackets.

In other example, given S = "))", the function should return 2,

because:

the first two characters of S, "))", contain zero

opening brackets, and

there are no remaining characters, so they contain

also zero closing brackets.

- -1of 1 vote
Given two strings representing very large integer numbers, find the Product. Do not use BigInt.

`using System; public class Program { public static void Main() { Console.WriteLine( GetProduct("18888888888888888888888888","19999999999999999999999999999999999999999999999999999999999")); } public static string GetProduct(string s1,string s2) { int digit1=0; int digit2=0; char[] str1=s1.ToCharArray(); char[] str2=s2.ToCharArray(); int carry; string[] result=new string[str1.Length]; string finalproduct=string.Empty; int padding=0; for(int i=str1.Length-1;i>=0;i--) { digit1=str1[i]-'0'; string resval=string.Empty; carry=0; int j; for(j=str2.Length-1;j>=0;j--) { digit2=str2[j]-'0'; int product=digit1*digit2+carry; carry=product/10; resval=resval+(product%10); } if(carry>0) { resval=resval+carry; } for(int k=0;k<padding;k++) { resval="0"+resval; } result[i]=resval; padding++; } int max=findMax(result); int count=0; int car=0; while(count<max) { int sum=0; for(int x=0;x<result.Length;x++) { if(count<result[x].Length) { sum+=result[x][count]-'0'; } } sum+=car; car=sum/10; int p=sum%10; finalproduct=p+finalproduct; count++; } finalproduct=car+finalproduct; return finalproduct.TrimStart('0'); } public static int findMax(string[] s) { int max=0; for(int i=0;i<s.Length;i++) { int len=s[i].Length; max=Math.Max(max,len); } return max; }`

}