## Merging arrays

Thanks to interviewpattern.com I discovered that one of the classical Amazon interview questions is writing a snippet of code to merge two sorted arrays:

"Suppose we have two sorted arrays A[] of m elements and B[] of n elements. Write a function merge which would merge this two arrays into new sorted array C[] in O(n) time as shown on the picture".

This problem is also a classical exercise for functional programming learners that shows the conciseness of functional code in comparison with imperative one.

The solution presented on the original page is written in C# and is longer than 40 lines of code, while we can solve the same problem in F# with less than 10 lines:

let rec merge_arrays a b = match (a, b) with | (a, []) -> a | ([], b) -> b | (x::xs, y::ys) -> if (x < y) then (x :: (merge_arrays xs (y::ys))) else (y :: (merge_arrays (x::xs) ys))

Besides being shorter, I also find the functional code to be much easier to understand. Do you agree with me?

## Google Interview Question: Product of other Elements in an Array in O(n)

Last time I was interviewed for a software development engineer position, the recruiter asked me some of the *classical* **Microsoft interview questions**, such as "*How Would You Move Mount Fuji?*" or "*How many gas station are there in your country?*".

It was the first time for me to be asked such questions but having obtained the job I think my answers were good enough.

After that day, I looked for other well-known interview questions and I discovered that Google has a totally different approach, focusing on **algorithms**, **data structures** and **complexity**.

For instance, one of Google interview questions says:

There is an array A[N] of N integers. You have to compose an array Output[N+1] such that Output[i] will be equal to the product of all the elements of A[] except A[i].

Example:

INPUT:[4, 3, 2, 1, 2]

OUTPUT:[12, 16, 24, 48, 24]Solve it without division operator and in O(n).

Without the two constraints at the end of the puzzle it would have been straightforward, but now we have to be smart.

Actually, the product of all elements of *A[]* except *A[i]* is equal to the product of all elements before *A[i]* and those after *A[i]*.

We can traverse *A[]* twice, once from left to right and once in the opposite way and multiply all the elements we find before *A[i]*.

We'll pretend to have a new array called *Output[]* to store the output of the first pass, assigning *Output[i]* the product of all elements preceding *A[i]*:

let rec firstpass product input = match input with | [] -> [] | x::xs -> product :: firstpass (product * x) xs

For the second pass we need to move from right to left, but this can be done by reversing the input arrays and moving as usual:

let secondpass product input arr = let rev_input = List.rev input let rev_arr = List.rev arr let rec rev_secondpass product (input:list<int>) arr = match arr with | [] -> [] | x::xs -> rev_secondpass (product * input.Head) input.Tail xs @ [(x * product)] rev_secondpass product rev_input rev_arr

Both *firstpass* and *secondpass* expect an integer argument called *product*, which will be always be 1 at the beginning and will be used to store the intermediate products during the recursive calls.

With these functions we can just define an input array and apply them to get the result.

The following is the complete F# code:

#light let input = [ 4; 3; 2; 1; 2 ] let answer values = let rec firstpass product input = match input with | [] -> [] | x::xs -> product :: firstpass (product * x) xs let secondpass product input arr = let rev_input = List.rev input let rev_arr = List.rev arr let rec rev_secondpass product (input:list<int>) arr = match arr with | [] -> [] | x::xs -> rev_secondpass (product * input.Head) input.Tail xs @ [(x * product)] rev_secondpass product rev_input rev_arr values |> firstpass 1 |> secondpass 1 values