Stone Wall

You are going to build a stone wall. The wall should be straight and N meters long, and its thickness should be constant; however, it should have different heights in different places. The height of the wall is specified by a zero-indexed array H of N positive integers. H[I] is the height of the wall from I to I+1 meters to the right of its left end. In particular, H[0] is the height of the wall’s left end and H[N−1] is the height of the wall’s right end.

Initialise AngularJS application with server data

Sometime you need to init the data from server before you want the app to be loaded, if you use ng-app the Angular application always load before you can make ajax call to retrieve data. The solution for that is to remove the ng-app and bootstrap the Angular JS application programmatically after we got the data from server. But you need to handle the case the API or server is down and the data is never returned then the app will not be bootstrapped.

Number of distinct absolute values of sorted array elements

function solution(A) { // write your code in JavaScript (Node.js 4.0.0) var count=0;l=0,r=A.length-1; while(l <= r){ count++; if(Math.abs(A[l]) === Math.abs(A[r])){ l++; r–; } else if(Math.abs(A[l]) > Math.abs(A[r])){ l++; } else if(Math.abs(A[l]) < Math.abs(A[r])){ r–; } } return count; }

Chocolates By Numbers

Two positive integers N and M are given. Integer N represents the number of chocolates arranged in a circle, numbered from 0 to N − 1. You start to eat the chocolates. After eating a chocolate you leave only a wrapper. You begin with eating chocolate number 0. Then you omit the next M − 1 chocolates or wrappers on the circle, and eat the following one. More precisely, if you ate chocolate number X, then you will next eat the chocolate with number (X + M) modulo N (remainder of division).

Calculate how many fish are alive

You are given two non-empty zero-indexed arrays A and B consisting of N integers. Arrays A and B represent N voracious fish in a river, ordered downstream along the flow of the river. The fish are numbered from 0 to N − 1. If P and Q are two fish and P < Q, then fish P is initially upstream of fish Q. Initially, each fish has a unique position.