## Arrays Interview Questions

- 1of 3 votes

AnswersGiven array of length n, having element 0 to n-1.

- DATA April 11, 2017 in United States

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.| Report Duplicate | Flag | PURGE

Yahoo Backend Developer Arrays Data Structures Math & Computation Online Test - 0of 0 votes

AnswersGiven 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

- mcg1coding April 10, 2017 in United States| Report Duplicate | Flag | PURGE

Fidessa Software Developer Arrays - 0of 0 votes

AnswersOne question containing multiple questions

- sonesh April 07, 2017 in United States

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| Report Duplicate | Flag | PURGE

Bloomberg LP Software Engineer / Developer Arrays - -5of 5 votes

AnswersFill the arrray with elements from 0 to 9.

- algoLearner March 21, 2017 in India

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)| Report Duplicate | Flag | PURGE

Amazon Developer Program Engineer Arrays - 0of 0 votes

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

- algoLearner March 21, 2017 in United States

How will you read and write a matrix, with row and column number using STL.| Report Duplicate | Flag | PURGE

Arrays - 0of 0 votes

AnswersAmezon_interview_3rd round:

- algoLearner March 21, 2017 in United States

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)| Report Duplicate | Flag | PURGE

Arrays - -1of 1 vote

Answerusing System;

- sunil.sebastian March 16, 2017 in United States

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace ArrayProblems

{

public class MultiplyTwoLargeNumber

{

public static string MultiplyBigNumbers(string s1, string s2)

{

char[] num1 = s1.ToCharArray();

char[] num2 = s2.ToCharArray();

//string=99

//char--> 9 , val --> 57(48+9)

//so s[i]-'0' will give val 9 and char as horizontal tab

char[] result = new char[num1.Length + num2.Length]; // Default 0 '/0' 2 ==> 50 '2'

int carry = 0;

int offset = 0;

for (int i = num1.Length - 1; i >= 0; i--)

{

int tail = result.Length - 1 - offset;

for (int j = num2.Length - 1; j >= 0; j--)

{

int resval = 0;

if(result[tail]!=0)

{

resval = result[tail] - '0';

}

int sum = resval+ ((num1[i] - '0') * (num2[j] - '0')) + carry; //remember to add result before taking mode

result[tail] = (char)((sum % 10) + '0');

carry = sum / 10;

tail--;

}

if (carry > 0)

{

int res = (result[tail] != 0) ? (result[tail] - '0') + carry : result[tail] + carry;

result[tail] = (char)(res + '0');

carry = 0;

}

offset++;

}

return new string(result);

}

}

}| Report Duplicate | Flag | PURGE

Microsoft Software Engineer / Developer Arrays - 0of 0 votes

AnswersGiven a array of integers {-6,-3,-1,2,4,5} which are sorted .Sort square of the numbers .Output {1,4,9,16,25,36}

- John4jobs March 08, 2017 in United States| Report Duplicate | Flag | PURGE

Fungible Developer Program Engineer Arrays - 0of 4 votes

AnswersYou have L, a list containing some digits (0 to 9). Write a function answer(L) which finds the largest number that can be made from some or all of these digits and is divisible by 3. If it is not possible to make such a number, return 0 as the answer. L will contain anywhere from 1 to 9 digits. The same digit may appear multiple times in the list, but each element in the list may only be used once.

- Parth Patel February 21, 2017 in United States

{{

Test cases

==========

Inputs:

(int list) l = [3, 1, 4, 1]

Output:

(int) 4311

Inputs:

(int list) l = [3, 1, 4, 1, 5, 9]

Output:

(int) 94311

}}

My Solution:

{{

package com.google.challenges;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Collections;

public class Answer {

public static int answer(int[] l) {

// Your code goes here.

ArrayList<Integer> list0 = new ArrayList<>();

ArrayList<Integer> list1 = new ArrayList<>();

ArrayList<Integer> list2 = new ArrayList<>();

int sum =0;

Arrays.sort(l);

for(int i = 0; i<l.length; i++){

if(l[i] % 3 == 0){

list0.add(l[i]);

}else if(l[i] % 3 == 1){

list1.add(l[i]);

}else{

list2.add(l[i]);

}

sum += l[i];

}

if(sum%3==0){

StringBuilder strNum = new StringBuilder();

for(int i = l.length-1; i >= 0; i--)

{

strNum.append(l[i]);

}

return Integer.parseInt(strNum.toString());

}else if(sum%3 == 1){

if(list1.size()>0){

Collections.sort(list1);

list1.remove(0);

}else if(list2.size() >= 2){

Collections.sort(list2);

list2.remove(1);

list2.remove(0);

}else{

return -1;

}

}else if(sum%3 == 2){

if(list2.size()>0){

Collections.sort(list2);

list2.remove(0);

}else if(list1.size() >= 2){

Collections.sort(list1);

list1.remove(1);

list1.remove(0);

}else{

return -1;

}

}

list0.addAll(list1);

list0.addAll(list2);

StringBuilder strNum = new StringBuilder();

Collections.sort(list0);

for(int i = list0.size()-1; i >= 0; i--)

{

strNum.append(list0.get(i));

}

return strNum.length() > 0 ? Integer.parseInt(strNum.toString()) : -1;

}

}

}}

But here I am able to pass 4 test cases out of 5. Therefore I am looking for scenario which is left to check.

Can someone help me?| Report Duplicate | Flag | PURGE

Google Software Engineer Google FooBar 24x7 Google chrome technical support number 1-888-201-2039 Arrays Computer Science Java Problem Solving - -1of 1 vote

AnswersI was asked the following: Given integers N and A. Find how many integer sequences with elements between 1 and A have sum of all elements equals to N.

- merlinparrajimenez January 26, 2017 in United States

N, A <= 1000.

Sample input: 4 3 , sample output is 7.

In this moment, I realized I do not understand the question. If I have a sequence of 1,2,3, the only sub-sequence that sums 4 is 1,3. So the answer should be 1. What am I missing?

Thank you| Report Duplicate | Flag | PURGE

Algorithm Arrays - 1of 1 vote

AnswersGiven an array of integers, design an algorithm that moves all non-zero integers to the end of the array. Minimize the number of writes or swaps.

- pygrammer January 21, 2017 in United States| Report Duplicate | Flag | PURGE

Facebook Software Engineer Algorithm Arrays - 1of 1 vote

AnswersYou are given an array of integers(with all valid input) You have to write a function which will produce another array, where the value in each index of the array will be the product of all values in the given array accept that index.

- Azarbaizan January 10, 2017 in United States for Market Place

Example

Array 1: 1 2 3 4 5

Array 2: 120 60 40 30 24.

Come up with a solution of O(n^2) can you improve it?| Report Duplicate | Flag | PURGE

Amazon Software Engineer Arrays - 0of 0 votes

AnswersGiven an array of random numbers, shuffle the numbers once again with the least possibility of it being same as previous configuration.

- santakdalai90 December 20, 2016 in India| Report Duplicate | Flag | PURGE

EFI Software Engineer Arrays - 0of 0 votes

AnswersQ. Given an array of numbers. Print all the pairs (2) of numbers in the array if the sum of those numbers is also present in the array. Write in C

- bombaySE December 01, 2016 in United States| Report Duplicate | Flag | PURGE

Amazon Arrays - 1of 1 vote

AnswersReturn the pivot index of the given array of numbers. The pivot index is the index where the sum of the numbers on the left is equal to the sum of the numbers on the right. Input Array {1,2,3,4,0,6}

- umesh.shaw November 11, 2016 in United States| Report Duplicate | Flag | PURGE

Google Software Engineer Arrays - 0of 0 votes

AnswersYou are given an array of positive and negative integers. If a number n at an index is positive, then move forward n steps. Conversely, if it's negative, move backward n steps. Determine if there is a loop in this array.

- shreydesai@utexas.edu October 29, 2016 in United States

For example, given the array [2, -1, 1, 2, 2], index 0 maps to index 2, 1 maps to 0, 2 maps to 3, and so on. There is a loop in this array because 0 maps to 2, 2 maps to 3, and 3 maps to 0 (use the modulo operator).| Report Duplicate | Flag | PURGE

Microsoft Software Engineer Intern Arrays - 0of 0 votes

AnswersGiven an array, move the smaller no to the left and the larger nos to the right. The relative positioning between the small no's and the relative positions between the large nos should not change.

The original ( ill formulated ) question can be found here :

question?id=5756583549075456.

Example :`a = [ 6 4 5 0 2 1 11 -1 ] after_a = [ 0 , 2, 1, -1, 6, 4, 5, 11 ]`

Note, for lack of good explanation, please do not laugh at the poster in the solutions. After all, they are trying to help or get help.

- NoOne October 12, 2016 in United States| Report Duplicate | Flag | PURGE

Arrays - 0of 0 votes

AnswersCAREERCUP is a boad game hat contains m x n on a board. The objective of the CAREERCUP game is to reach the bottom of he board (bottom right corner) from the top of the board (top left corner) while moving one grid at a ime in either the down, right or diagonally downwrd directions.

- J@sper October 11, 2016 in United States

Write a method called CareerSolution that takes in two integers representing m and n, and returns the total number of ways a player can complete the game.

PS: Was later asked to optimize the solution.

int CareerSolution(int m, int n) {

}| Report Duplicate | Flag | PURGE

Amazon Intern Arrays - 1of 1 vote

AnswersLet's say I have a word "I love chicken", I can break the number of characters in each word, like so: [1] [4] [7]

- J@sper October 11, 2016 in United States

[1,4] [4,7], [1,4,7].

Now let's say I have a max = 5. The phrases with equal or fever than 5 characters are "[I], [love], and [I, love]. The longest phrase is [I,love], which contains 2 words.

Complete the Length function given. It has 2 parameters:

1) An array of integers, named array

2) A maximum number, named max.

int Careercup( int [] array, int max) {

}

Example test case 1:

[3,1,2,3]

4

Output expected : 2| Report Duplicate | Flag | PURGE

Google Software Developer Arrays - -2of 2 votes

AnswersGiven an array of numbers, move small numbers to start and big numbers to end of array

- rag August 07, 2016 in India| Report Duplicate | Flag | PURGE

SDET Arrays - 1of 1 vote

AnswersTwo friends Kohli and Dhoni want to test their friendship to check how compatible they are. Given a list of n movies numbered 1,2,3....n and asked both of them to rank the movies.

- abhibhagia August 07, 2016 in India

Design an algorithm to find compatibility difference between them.

Compatibility difference is the number of mis-matches in the relative rankings of the same movie given by them i.e. if Kohli ranks Movie 3 before Movie 2 and Dhoni ranks Movie 2 before Movie 3 then its a relative ranking mis-match Compatibility difference is the maximum number of mis-matches

Sample Input

5

31245

32415

Sample Output

2

Explanation

Movies are 1,2,3,4,5. Kohli ranks them 3,1,2,4,5, Dhoni ranks them 3,2,4,1,5. Compatibility difference is 2 because Kohli ranks movie 1 before 2,4 but Dhoni ranks it after.| Report Duplicate | Flag | PURGE

Walmart Labs Senior Software Development Engineer Arrays - 2of 2 votes

AnswersFind the length of maximum number of consecutive numbers jumbled up in an array.

- mrityunjay21 July 26, 2016 in India for Payments

e.g.: 1, 94, 93, 1000, 2, 92, 1001 should return 3 for 92, 93, 94| Report Duplicate | Flag | PURGE

Amazon SDE-2 Arrays - 0of 0 votes

AnswersA matrix is "Toepliz" if each descending diagonal from left to right is constant. Given an M x N matrix write the method isToepliz to determine if a matrix is Toepliz.

- enkadi13 July 22, 2016 in United States

Example:

Input:

67892

46789

14678

01467

Output:

True| Report Duplicate | Flag | PURGE

Google Software Engineer Algorithm Arrays - 0of 0 votes

AnswersPrint the element in anti spiral order

- Siva Vishal July 03, 2016 in India

for example

Matrix

1 2 3

4 5 6

7 8 9

o/p

5 6 9 8 7 4 1 2 3| Report Duplicate | Flag | PURGE

Zoho Student Arrays - 0of 2 votes

AnswersThere are 2 arrays of integers.You have to add the those integers and keep it in 3rd array.there is one condition, if the sum is a 2 digit number, split that number into single digiit and other condition is if any of the array integer is left then print that number

- Ajay April 05, 2016 in India for amazon.in

I/P:

int[] a = {1,2,3,4,5,6}

int[] b = {2,3,4,5,6,7,8}

o/p:

{3,5,7,9,1,1,1,3,8}| Report Duplicate | Flag | PURGE

Amazon Quality Assurance Engineer Arrays - 0of 0 votes

AnswersGiven a histogram chart with values say {5,4,3,6,0,1}. Get the total count required to completely melt the histogram. A column with value 5 has 5 blocks in it. Any block which has air on any of its side gets melted.

- abc_abc March 07, 2016 in United States

Sample 1

{5,4,3,6,0,1} - > {0,3,2,0,0,0}->{0,0,0,0,0,0} => count=2

Sample 2

{0,1,1,1,1,0} - > {0,0,0,0,0,0} => count=1| Report Duplicate | Flag | PURGE

Goldman Sachs Software Engineer Arrays - 0of 0 votes

AnswersAllocate a 2-D array of size m*n using malloc(). The array should be accessible as a[i][j].

- Saurabh Singhal January 16, 2016 in India| Report Duplicate | Flag | PURGE

Arista Networks Software Engineer Arrays C Data Structures Matrix - 0of 0 votes

AnswersGiven an array of stock values of a company. Find out the time when a user would have bought the stock and sold the sock. Basically find the maximum positive difference of any two given elements in an array?

- avinash.setty December 12, 2015 in United States for Marketplace| Report Duplicate | Flag | PURGE

Amazon SDE-2 Arrays

**CareerCup**is the world's biggest and best source for software engineering interview preparation. See all our resources.

Open Chat in New Window