## Arrays Interview Questions

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

- 0of 0 votes
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; }`

}

- 0of 0 votes
Given 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}

- -2of 2 votes
You 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.

{{

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?

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

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

- 0of 0 votes
Given 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.

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

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?

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

- 0of 0 votes
Q. 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

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

- 0of 0 votes
You 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.

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).

- 0of 0 votes
Given 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.

- 0of 0 votes
CAREERCUP 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.

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) {

}

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

[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

- 0of 0 votes
you have a array nums as input. For any i from 0 to length - 1. should print product of whole array except nums[i]

For example: nums = [2,3,1,4,3,2]

output:

72

48

144

36

48

72

- -2of 2 votes
Given an array of numbers, move small numbers to start and big numbers to end of array

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

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.

- 2of 2 votes
Find the length of maximum number of consecutive numbers jumbled up in an array.

e.g.: 1, 94, 93, 1000, 2, 92, 1001 should return 3 for 92, 93, 94

- 0of 0 votes
A 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.

Example:

Input:

67892

46789

14678

01467

Output:

True

- 0of 0 votes
Print the element in anti spiral order

for example

Matrix

1 2 3

4 5 6

7 8 9

o/p

5 6 9 8 7 4 1 2 3

- 0of 2 votes
There 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

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}

- 0of 0 votes
Given 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.

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

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

- 0of 0 votes
Given 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?

- 0of 0 votes
We have a file path as like this:

wchar_t* pCompletePath = L"\\?\UNC\10.1.3.23\TOKEN$\0x464564564576\C\FileDrive\Testcase.doc";

Write a C++ function which will take input argument as above string and gives output as L”\\?\UNC\10.1.3.23\TOKEN$\C\FileDrive\Testcase.doc” (Removing one token after the TOKEN$)

Constraints: TOKEN$ will be there for all strings, so we can use that for search

- 1of 1 vote
WAP in java to find duplicate element in array in one scan.

- -2of 2 votes
write a method that takes in 2 int arrays of any size and returns an array that calculates the sum of both.

for example, [1,2,3] and [2,3,4] will return [3,5,7]

Or [1,2,3] and [2,3,5,5] will return [2,4,7,8]

however, if it's like [9,9,2] and [0,1,3] you need to carry the sum so it returns as [1,0,0,5]

** SINGLE DIGIT ONLY