## Arrays Interview Questions

- 0of 0 votes
You are given a matrix where some pixels are white and some are black. Basically there are different disjoint images in the matrix.

a) Expand/Shrink the images

b) Count the no of images

c) Color the images

d) Rotate the images

- 0of 0 votes
The input is a sequence x1,x2,...,xn of integers in an arbitrary order, and another sequence

a1,a2,..,an of distinct integers from 1 to n (namely a1,a2,...,an is a permutation of

1, 2,..., n). Both sequences are given as arrays. Design an 0(n logn) algorithm to order

the first sequence according to the order imposed by the permutation. In other words, for

each i, Xi should appear in the position given in ai. For example, if x = 17, 5, 1,9, and a =

3, 2, 4, 1, then the outcome should be x = 9, 5, 17, 1. The algorithm should be in-place, so

you cannot use an additional array.

- 0of 0 votes
give me the code for :

Given a string say "I am a human being" the output should reverse all letters of each word but not the whole string as such.

Eg: O/p should be "I ma a namuh gnieb"

I somewhat wrote the code, but i was asked what if there are extra spaces etc.

(i am able to write the code sitting at my desktop at one short but there front of interviewer i am struggling. Need to build up my confidence)

let me know the best and optimised way of writing this code.

Also i suggest people to aviod using inbuilt functions as much as possible

My Answer is as below in perl`#i want the reverse of the letters of all words in a string #eg Input is "I am a human being" then o/p shud be "I ma a namuh gnieb" $str="I am a human being"; @arr=split(' ',$str); print @arr; for($i=@arr-1;$i>=0;$i--) { $_=@arr[$i]; ####intead of above for loop if we use foreach(@arr) then it will reverse the whole string @word=split('',$_); { foreach $n (@word) { unshift(@final,$n); } } } print "\n @final \n";`

- 0of 0 votes
give me a code to find all anagrams or combnations of a given work.

Say if the word given was "hello"

then

hel

he

hell

leho

lleho

and so on

- 1of 1 vote
Given an array say [0,1,2,3,5,6,7,11,12,14,20]

given a number say 5.

Now find the sum of elements which sum to 5

eg:2+3=5, 0+5=5 etc.

I guess the interviewer wanted all possible combinations eg 0+2+3=5, etc

- 0of 0 votes
Array of Integers with even number of same Integers. Find the Integer that is an odd number of times. Compare efficiency between different approaches.

- 0of 0 votes
Given a couple of integer arrays A = {2, 4, 3, 5, 6, 8} & B = {9, 2, 7, 6} - Return the intersection of these arrays.

Once I provided a solution (which was n squared -O (n^2)) he followed up by asking me if I could make it linear (O(n)).

- 0of 0 votes
Given a larger integer buffer/array (say size, x), now given a window size (say, n) and a number (say, k). Windows starts from the 1st element and keeps shifting right by one element. The objective is to find the minimum k numbers present in each window.

- -3of 3 votes
Having trouble with this array pair difference problem (NOT array pair sum) because of a certain edge case.

Example is: k = 4 a = [ 1, 1, 5, 6, 9, 16, 27] output: 3 (Due to 2x [1, 5], and [5, 9])

So, find the difference that equals to k. I used this code in my interview but realized it was wrong hours later unfortunately. It only gives 2.`public static int arrayPairDifference(int[] a, int k) { HashMap<Integer, Integer> hashMap = new HashMap<>(); int count = 0; for (int i = 0; i < a.length; i++) { if (hashMap.containsValue(a[i] - k)) { count++; } hashMap.put(i, a[i]); } return count; }`

How to account for the edge case of the 2x [1, 5] ?

- 0of 0 votes
Move the first n numbers in a 10 element array to the end.

I think the way to do it was to reverse the array and then reverse the first 10-n and then the last n.

- 2of 2 votes
You are given an array with numbers - [11, 3, 11, 11, 3, 2, 0, -2, 2]

You are supposed to write a function that returns the number that appears "odd" number of times.

The solution is obviously using HashMap. But that takes O(n) to create the HashMap and O(n) to lookup. How can one eliminate the second O(n) yet keeping the HashMap?

Hint: Do you really need to count frequency of occurrence of each digit?

- 0of 0 votes
Given a number in an array form, Come up with an algorithm to push all the zeros to the end.

Expectation : O(n) solution

- 0of 0 votes
Give 2 arrays of size 7 and 3 which are sorted such that the last 3 blocks in first array are empty, merge the arrays in a sorted manner in the most efficient way.

E.g:-

a[7] = [4, 10, 11, 20__, __, __]

b[3] = [1,3,7]

- 0of 0 votes
public interface InfluencerFinder {

/**

* Given a matrix of following between N LinkedIn users (with ids from 0 to N-1):

* followingMatrix[i][j] == true iff user i is following user j

* thus followingMatrix[i][j] doesn't imply followingMatrix[j][i].

* Let's also agree that followingMatrix[i][i] == false

*

* Influencer is a user who is:

* - followed by everyone else and

* - not following anyone himself

*

* This method should find an Influencer by a given matrix of following,

* or return -1 if there is no Influencer in this group.

*/

int getInfluencer(boolean[][] followingMatrix)

- 0of 0 votes
if you are given 2 arrays

one has n elements and another has n+2 elements

and the elements in the array are same except the 2 elements

find those two extra elements..

give the optimal solution.

- 0of 2 votes
Given an array of n distinct integers sorted in ascending order. Find an index i s.t ar[i] = i. Return -1 if no such index exists. Note that integers in array can be negative.

- 1of 1 vote
Write a program for finding a minimum element in rotated sorted array(either ascending or descending ) and array contains duplicates.

- 0of 0 votes
Write a program find minimum element in the rotated sorted array( ascending or descending ) and array having duplicates elements.

- 0of 0 votes
given 2 Dimensional array

I/P -- String[][] input = { { "abc", "def", "gh" },

{ "f", "g" },

{ "qrt","xyz","pqr" } };

Program shd return a 2-D Array with

O/P -- { { "abcfqrt", "abcfxyz", "abcfpqr" ,abcgqrt and so on ..

- 1of 1 vote
Given a 3-D array, if any m[r][c][d] is <=0 mark all the cells in the entire row,col and depth as zero and return the o/p array

- 0of 0 votes
`suppose u r given a 4*3 rectangle like (take these values from user)Now u have to calculate the no. of squares in this rectangle like:No. of squares of dimension 1 is 12No. of squares of dimension 2 is 6No. of squares of dimension 3 is 2No. of squares of dimension 4 is 0Total no. of squares are 2`

- 0of 0 votes
suppose u r given a 4*3 rectangle like (take these values from user)Now u have to calculate the no. of squares in this rectangle like:No. of squares of dimension 1 is 12No. of squares of dimension 2 is 6No. of squares of dimension 3 is 2No. of squares of dimension 4 is 0Total no. of squares are 2

- 1of 1 vote
`In given array find zero and replace the entire row and column with zeros E.g Input: 1 2 3 4 5 6 7 8 9 10 0 11 12 13 14 15 Output: 1 2 0 4 5 6 0 8 0 0 0 0 12 13 0 15`

- 0of 0 votes
How to find the adjacent elements of an single dimensional array whose difference is 1; What is the Time Complexity & Auxiliary Space you use ? What type of efficiency can be achieved?

- 0of 0 votes
How to sort an single dimensional array whose elements are {4,5,6,4,5,6}; What is the Time Complexity & Auxiliary Space you use ? What type of efficiency can be achieved?

- 1of 1 vote
Given a sorted array of integers, write a function that will return the number with the biggest number of repetitions.

(Asked to refine the solution to be more efficient)

- 0of 0 votes
Using the following function signature, write a C# function that prints out every combination of indices using Console.WriteLine() whose values add up to a specified sum, n. Values of 0 should be ignored.

public void PrintSumCombinations(List<int> numbers, int n);

• It’s okay to use additional private functions to implement the public function

• Be sure to print out the indices of numbers and not the values at those indices

• Don’t worry too much about memory or CPU optimization; focus on correctness

To help clarify the problem, calling the function with the following input:

List<int> numbers = new List<int> { 1, 1, 2, 2, 4 };

PrintSumCombinations(numbers, 4);

Should result in the following console output (the ordering of the different lines isn’t important and may vary by implementation):

0 1 2 (i.e. numbers[0] + numbers[1] + numbers[2] = 1 + 1 + 2 = 4)

0 1 3

2 3

4

- 1of 1 vote
Given a sorted array with duplicates, move the distinct elements to the top

Ex: 1,1,2,3,4,4,5 -> 1,2,3,4,5

- 0of 0 votes
Given an array with numbers, your task is to find 4 numbers that will satisfy this equation:

A + B + C = D

- 0of 0 votes
Find the maximum-sum subarray of an array.