Picture of Brian Love wearing black against a dark wall in Portland, OR.

Brian Love

Array Left Rotation Using Javascript and Node.js

I’ve put together some sample solutions to the common array left rotation challenge.

If you have not heard of hackerrank.com, check it out. It allows you to tackle common challenges using a variety of programming languages. The array left rotation challenge involves accepting some basic input via standard input, performing the array left rotation using the input, and then outputting the result as expected.

Here is the overview from hackerrank:

A left rotation operation on an array of size shifts each of the array’s elements unit to the left. For example, if 2 left rotations are performed on array [1, 2, 3, 4, 5], then the array would become [3, 4, 5, 1, 2].

The tests expect an output of each element in the array separated by a single space. So, using the example above the expected output is: 3 4 5 1 2.

Source

You can download the source code and follow along or fork the repository on GitHub:

Input

The first step I took was to create a simple text file that represents the given input:

5 4
1 2 3 4 5

I saved this to a file named data.txt.

I then wrote some code using Node.js to read and parse the data:

//global variables
let n = 0;
let d = 0;
let data = [];

//read standard input
process.stdin.setEncoding('utf8');
process.stdin.resume();

//store input
let input = '';
process.stdin.on('data', function (data) {
  input += data;
});
process.stdin.on('end', function () {
  let linesOfInput = input.split('\n');
  let temp = linesOfInput[0].split(' ');
  temp.map(Number);
  n = temp[0];
  d = temp[1];
  data = linesOfInput[1].split(' ');

  main();
});

//let's do it
function main() {
  //verify values
  if (n <= 0 || d <= 0 || data.length > n || data.length < d) {
    throw Error('The input values are invalid.');
  }

  //get result
  //this is a new array after the left shifts
  let result = getResult();

  //output result
  process.stdout.write(result.join(' '));
}

First, I define some globally available variables:

Next, I read in the data from the standard input. When we are finished reading the standard input stream I parse the data to set the value of n, d and data. Finally, I invoke the main() function and we’re off.

First: for-loop

The first approach I took was to use the for-loop to iterate the number of shifts, and then to perform a single shift of the array data.

function main() {
  //code omitted

  //get result
  let result = getResultsUsingLoop();

  //code omitted
}

function getResultsUsingLoop() {
  let leftRotate = function (result) {
    let temp = [];
    for (var i = 0; i < n - 1; i++) {
      temp[i] = result[i + 1];
    }
    temp[i] = result[0];
    return temp;
  };

  let result = data.splice(0);
  for (let i = 0; i < d; i++) {
    result = leftRotate(result);
  }

  return result;
}

The main() function as not changed other than invoking the getResultsUsingLoop() function to get the resulting array.

A quick explanation of this approach:

My evaluation of this approach:

Second: array.map()

function main() {
  //code omitted

  //get result
  let result = getResultUsingArrayMap();

  //code omitted
}

function getResultUsingArrayMap() {
  let result = data.map(function (value, index) {
    let pos = parseInt(index) + parseInt(d - 1);
    if (pos > n - 1) {
      pos = pos - n;
    }
    return data[pos];
  });

  return result;
}

A quick explanation of this approach:

My evaluation of this approach:

Third: array.shift()

function main() {
  //code omitted

  //get result
  let result = getResultsUsingArrayShift();

  //code omitted
}

function getResultsUsingArrayShift() {
  let temp = data.splice(0);
  for (let i = 0; i < d - 1; i++) {
    let first = temp.shift();
    temp.push(first);
  }
  return temp;
}

As I thought more about the problem I realized that I was not taking advantage of Array.prototype methods like shift() and push().

A quick explanation of this approach:

My evaluation of this approach:

Source

You can download the source code or fork the repository on GitHub:

Executing

Executing the solution is easy:

$ node array-left-rotation.js < data.txt