# coding interview

## The most asked coding interview - #5

Transform array such that each element contains the greatest element on its right side Problem statement In this article we will be discussing how to solve an easy problem that has been asked at Amazon coding interviews. This is a must do problem for your coding interview preparation. The problem statement goes as follows: You are given an array A of size N. You have to modify A in place s.t. A[i] = M where M is the maximum value among all elements A[j], j > i. If such element does not exists set A[i] = -1. Example Solution Since this is a quite easy… Read More »The most asked coding interview - #5

# Return the Maximum absolute difference of values and indices

In this article we will be discussing one of the most asked coding interview question asked at Amazon.
The problem statement goes as follows:

## Problem statement

Given an array A determine the maximum possible value of the following expression:
$f(i,j) =| A[i]-A[j] | +|i-j|$

## Example

Input : A = {1, 3, -1}
Output : 5
because the maximum value obtainable is:
=> f(1,2) = |3 - (-1)| + |1-2| = 5

Input : A={3, -2, 5, -4}
Output : 10
=> f(1, 4) = f(4, 1) = |3 - (-4)| + |1 - 4| = 10
Note that in this case we can also obtain 10 from
=> f(3, 4) = f(4, 3) = |5 - (-4)| + |3 - 4| = 10

Read More »The most asked coding interview - #4

Sort an array of size N made of numbers from 0 to K Problem statement In this article we will be discussing another (see the previous article) very popular programming interview question. This time we are asked to sort an array of size N whose elements are in the range [0,K). Problem statement Given an array $A=[a_1,a_2,…a_n],\: 1 \leq a_i \leq K$ modify A s.t. it is sorted. As usual you should ask your interviewer some clarifying questions. For example a sensible question to ask could be: What is the maximum value of $K$ and of $N$? Let's assume that $N \leq 10^7$ and $K\leq… Read More »The most asked programming interview questions - #3 ## The most asked coding interview - #2 # Coding interview question: Determine if number is an Armstrong number ## Problem statement In this article we will be discussing another (see the previous article counting triples with a given sum) of the most common coding interview questions. The problem statement is really simple and goes as follows: Given an positive integer N determine if it is an Armstrong number? A number$x_1x_2…x_n$(1 \leq x_i \leq 9$ is a digit ) of length $n$ is an Armstrong number if the following is true:
$x_nx_{n-1}…x_2x_1 = pow(x_1,n) + pow(x_2,n) + … pow(x_n,n)$

In other words if raising all digits to power $n$ and sum all them up you obtain the original number, then $N$ is an Armstrong number.

Read More »The most asked coding interview - #2

## Apply a permutation to a vector

In this article we are going to discuss an interview question that is asked quite frequently in companies like Facebook, Microsoft, or Google. The problem statement goes as follows: Given an vector<T> A of elements of type T of length Nand a vector<size_t> P where Pis a permutation of numbers from [0-N-1]you need to apply the permutation Pto the vector A. You are not allowed to use more than O(1) additional space. For example suppose A=['a','b','c','d'] and P=['2','0','4','1']. After applying P to A, the latter becomes: ['c','a','d','b']. P[i] basically tell you which element of A should go in position i in the final arrangement of… Read More »Apply a permutation to a vector