Arrays Interview Questions
- 5of 5 votes
AnswersGiven 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
- Greg September 04, 2014 in United States
Please provide as efficient code as you can.
Can you better than this ???| Report Duplicate | Flag | PURGE
Amazon Software Engineer / Developer Algorithm Arrays C++ Coding - 0of 0 votes
AnswersGiven 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.
- sudeep.chauhan24 September 01, 2014 in India| Report Duplicate | Flag | PURGE
Amazon Software Engineer / Developer Arrays - 0of 0 votes
AnswersGiven 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.
- Saurabh2816 August 17, 2014 in India| Report Duplicate | Flag | PURGE
Amazon Intern Arrays - 0of 0 votes
AnswersWrite a function that accepts an n-dimension array and prints its values--For array of any dimension.
- farzanmoofty August 12, 2014 in United States for Price history
What is the layout of multi-dimensional array in the memory?| Report Duplicate | Flag | PURGE
Bloomberg LP Software Engineer / Developer Arrays C++ Coding - 0of 0 votes
AnswersWhat 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?
- farzanmoofty August 12, 2014 in United States for Price history| Report Duplicate | Flag | PURGE
Bloomberg LP Software Engineer / Developer Arrays C++ Coding Linked Lists - 0of 2 votes
AnswersDesign 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 .
- gopi.komanduri August 02, 2014 in United States| Report Duplicate | Flag | PURGE
Computer Scientist Algorithm Android Application / UI Design Arrays Bit Manipulation C# C++ Cache Coding Computer Architecture & Low Level Data Mining Data Structures Database Distributed Computing Dynamic Programming Hash Table Java Large Scale Computing Linked Lists Math & Computation Object Oriented Design Problem Solving Sorting SQL Stacks System Design Trees and Graphs XML - 0of 0 votes
AnswersDesign a phone book such that fields are searchable with name , with number. Later enhanced teh question asking searchable with address as well.
- gopi.komanduri July 22, 2014 in India| Report Duplicate | Flag | PURGE
Analyst Algorithm Arrays Bit Manipulation Brain Teasers C# C++ Cache Coding Computer Architecture & Low Level Data Mining Data Structures - 0of 2 votes
AnswersHow 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.
- gopi.komanduri July 05, 2014 in India| Report Duplicate | Flag | PURGE
Analyst Algorithm Arrays C# C++ Coding Data Structures Dynamic Programming Experience Hash Table Large Scale Computing Linked Lists Problem Solving Sorting Trees and Graphs - 1of 3 votes
AnswersDesign 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.
- gopi.komanduri July 04, 2014 in India
later added more complexity like do the same for organisation where even it contains designations. so this should be searchable with designations.| Report Duplicate | Flag | PURGE
Analyst Algorithm Arrays C C++ Cache Coding Computer Architecture & Low Level Data Mining Data Structures Dynamic Programming Hash Table Ideas Large Scale Computing Linked Lists Object Oriented Design Problem Solving Trees and Graphs - 2of 2 votes
Answers(Bar Raiser Round)
- gdg June 26, 2014 in United States
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| Report Duplicate | Flag | PURGE
Amazon Arrays - -4of 6 votes
AnswersYou 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.
- gdg June 21, 2014 in United States for Bing
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| Report Duplicate | Flag | PURGE
Google Developer Program Engineer Arrays - 2of 2 votes
AnswersCode 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.
- diegum June 06, 2014 in United States for iOS| Report Duplicate | Flag | PURGE
Facebook Software Engineer / Developer Arrays - 0of 0 votes
AnswersYou 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.
- northernlight May 06, 2014 in United Kingdom
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| Report Duplicate | Flag | PURGE
Amazon Algorithm Arrays C C++ - 1of 1 vote
AnswersYou are given a matrix where some pixels are white and some are black. Basically there are different disjoint images in the matrix.
- hulk April 20, 2014 in India
a) Expand/Shrink the images
b) Count the no of images
c) Color the images
d) Rotate the images| Report Duplicate | Flag | PURGE
Amazon Software Engineer / Developer Arrays - 6of 6 votes
AnswersThe input is a sequence x1,x2,...,xn of integers in an arbitrary order, and another sequence
- Coder April 17, 2014 in United States
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.| Report Duplicate | Flag | PURGE
Google Software Engineer / Developer Arrays - 2of 2 votes
Answersgive 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_learn April 11, 2014 in India#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";
| Report Duplicate | Flag | PURGE
Amazon Testing / Quality Assurance Algorithm Android Application / UI Design Arrays Automata Coding Data Structures Dynamic Programming Perl - 0of 0 votes
Answersgive me a code to find all anagrams or combnations of a given work.
- i_learn April 11, 2014 in India
Say if the word given was "hello"
then
hel
he
hell
leho
lleho
and so on| Report Duplicate | Flag | PURGE
Amazon Testing / Quality Assurance Algorithm Android Application / UI Design Arrays test Testing - 0of 2 votes
AnswersGiven an array say [0,1,2,3,5,6,7,11,12,14,20]
- i_learn April 11, 2014 in India
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| Report Duplicate | Flag | PURGE
Amazon Testing / Quality Assurance Algorithm Android Application / UI Design Arrays Coding Data Structures Dynamic Programming Perl Sorting test Testing - 0of 0 votes
AnswersArray of Integers with even number of same Integers. Find the Integer that is an odd number of times. Compare efficiency between different approaches.
- georgiosziazopoulos April 03, 2014 in Luxembourg| Report Duplicate | Flag | PURGE
Amazon Software Engineer / Developer Arrays - 0of 0 votes
AnswersGiven a couple of integer arrays A = {2, 4, 3, 5, 6, 8} & B = {9, 2, 7, 6} - Return the intersection of these arrays.
- Jeanclaude March 19, 2014 in United States
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)).| Report Duplicate | Flag | PURGE
Adobe Software Engineer / Developer Arrays - 0of 0 votes
AnswersGiven 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.
- nemesis07 March 14, 2014 in India| Report Duplicate | Flag | PURGE
Amazon Arrays - -3of 3 votes
AnswersHaving 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] ?
- Johnb March 12, 2014 in United States| Report Duplicate | Flag | PURGE
Algorithm Arrays Hash Table - 1of 1 vote
AnswersMove the first n numbers in a 10 element array to the end.
- FrickenHamster March 03, 2014 in United States for Lync
I think the way to do it was to reverse the array and then reverse the first 10-n and then the last n.| Report Duplicate | Flag | PURGE
Microsoft Software Engineer Intern Arrays - 2of 2 votes
AnswersYou are given an array with numbers - [11, 3, 11, 11, 3, 2, 0, -2, 2]
- KevinK February 27, 2014 in United States
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?| Report Duplicate | Flag | PURGE
Amazon Principal Software Engineer Arrays - 1of 1 vote
AnswersGiven a number in an array form, Come up with an algorithm to push all the zeros to the end.
- kiranpm86 February 24, 2014 in India
Expectation : O(n) solution| Report Duplicate | Flag | PURGE
Amazon Quality Assurance Engineer Algorithm Arrays C++ Coding - -1of 1 vote
AnswersGive 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.
- kiranpm86 February 24, 2014 in India
E.g:-
a[7] = [4, 10, 11, 20__, __, __]
b[3] = [1,3,7]| Report Duplicate | Flag | PURGE
Amazon Quality Assurance Engineer Algorithm Arrays C++ Coding - 0of 0 votes
Answerspublic interface InfluencerFinder {
- Anon February 19, 2014 in United States
/**
* 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)| Report Duplicate | Flag | PURGE
Linkedin Software Engineer / Developer Arrays - 0of 0 votes
Answersif you are given 2 arrays
- chugh.rachita February 15, 2014 in India
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.| Report Duplicate | Flag | PURGE
Intern Arrays - 0of 2 votes
AnswersGiven 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.
- gdg February 02, 2014 in United States| Report Duplicate | Flag | PURGE
Arrays - 9of 9 votes
AnswersWrite a program for finding a minimum element in rotated sorted array(either ascending or descending ) and array contains duplicates.
- kesar February 02, 2014 in United States| Report Duplicate | Flag | PURGE
Algorithm Arrays C Sorting C#