## Arrays Interview Questions

- 4of 4 votes
Given three arrays A,B,C containing unsorted numbers. Find three numbers a, b, c from each of array A, B, C such that |a-b|, |b-c| and |c-a| are minimum

Please provide as efficient code as you can.

Can you better than this ???

- 0of 0 votes
Given an array of positive and negative numbers(no zeroes),i have to arrange them in such a way that the positive and negative numbers should be arranged consecutively.The number of positive and negative numbers may not be equal i.e. if there is no positive number(or negative) left,then all the remaining negative numbers(or positive) are appended to the end of the array.The order is important i.e.if input array is { 2,-1,-3,-7,-8,9,5,-5,-7},then the output array should be {2,-1,9,-3,5,-7,-8,-5,-7}.The code is done in O(n) without using another array.I came up with a solution in which i chose 0 as pivot element and separate the numbers (using quicksort) but in this case the order is not preserved.

- 0of 0 votes
Given a sorted array of 0’s and 1’s. Find out the no. of 0’s in it. Write recursive, iterative versions of the code.

- 0of 0 votes
Write a function that accepts an n-dimension array and prints its values--For array of any dimension.

What is the layout of multi-dimensional array in the memory?

- 0of 0 votes
What does an iterator in C++ point to in case of a vector vs. list. Where would it point to if the prior links are deleted in the list? In case of a vector if it points to a specific index, where would it point to if the prior indexes are deleted?

- 0of 0 votes
Design a system like friend's functionality in facebook. should have all features of facebook's friends functionality. like for each person , he can have any number of friends , he will get suggestions for new firends , showing common friends if we visits any other profile . algo should be scalable , robust .

- 0of 0 votes
Design a phone book such that fields are searchable with name , with number. Later enhanced teh question asking searchable with address as well.

- 1of 1 vote
How to design a multi key hash map ( key count can be dynamic. if there are two keys , initiallly which can be used to find the value , keys can be increased to three as well ex: consider school structure. Intially , consider , student id is key , later , should be searchable even with key name , later with grade.

- 1of 1 vote
Design a telephone directory for large ppl (he gave example like design for India). fields will be , first name , last name , number . this should be searchable with first name , last name , number as welll.

later added more complexity like do the same for organisation where even it contains designations. so this should be searchable with designations.

- 1of 1 vote
(Bar Raiser Round)

Divide the array(+ve and -ve numbers) into two parts such that the average of both the parts is equal.

Input:

[1 7 15 29 11 9]

Output:

[15 9 1 7 11 29]

Explanation:

The average of first two elements is (15+9)/2 = 12, average of remaining elements is (1+7 +11 +29)/4 = 12

- -5of 7 votes
You have an array of integers(size N), such that each integer is present an odd number of time, except 3 of them(which are present even number of times). Find the three numbers.

Only XOR based solution was permitted.

Time Complexity: O(N)

Space Complexity: O(1)

Sample Input:

{1,6,4,1,4,5,8,8,4,6,8,8,9,7,9,5,9}

Sample Output:

1 6 8

- 1of 1 vote
Code a function that receives an array with duplicates and returns a new array keeping the original order of the elements but with the duplicates removed.

For example, if the input were`@[ @"dog", @"cat", @"dog", @"fish" ]`

the output would be

`@[ @"dog", @"cat", @"fish" ]`

Tell the complexity of the solution.

- 0of 0 votes
You are given an array of N elements. Each element in the range Min of int to Max of Int. You need to find the length of longest sequence in this array such that difference of largest and smallest element of that sequence is 1. The sequence need not be sequential.

For e.g. array[]={6,10,6,7,8,9,0}

seq {6,10} = diff is 4 len 2

seq { 10,7,8} diff is 3 len 3

seq { 7,8,9} diff 2 len 3

seq {6,6,7} diff is 1 len 3

In this example the program should return 3 .

Complexity N*longN

- 1of 1 vote
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

- 5of 5 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.

- 1of 1 vote
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.