Dan D Kim

Let's share stories

Node JS Template for Google Code Jam and Kick Start

2020-04-27 Dan D. Kimjavascript

TLDR: Here is the Node JS template for Google Code Jam and Kick Start: https://gitlab.com/snippets/1970354

  1. Copy-paste the code snippet into app.js
  2. Copy your input into input.txt
  3. Run node app.js < input.txt
function readInput() {
  const readline = require('readline')
  const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
    terminal: false
  })

  let problem = {
    T: 0,
    testCases: []
  }

  rl.on('line', function (line) {
    // TODO: Process input
    if (problem.T === 0) {
      // Get number of test cases from first line
      problem.T = Number(line)
    } else {
      // TODO process the rest of the data
      const [a, b] = line.split(' ')
      const aNum = Number(a)
      const bNum = Number(b)

      problem.testCases.push([aNum, bNum])
    }
  })

  .on('close', () => {
    // Finished processing input, now solve question
    solveProblem(problem)
    process.exit()
  })
}

readInput()

Are you planning on trying out Google's coding competitions?

If it’s your first time, you might come across the same confusion that I did on my Google Code Jam.

That is, how do you work with standard input and output on this platform? It’s not like Leetcode where you simply need to define a function that takes in a pre-defined object.

It’s not like Leetcode

Take a look at the following question:

Consider the following very simple problem. The input consists of one line with a number of test cases T, and then T more lines, each of which contains two positive integers N and M. The desired output form is Case #x: y z, where x, y, and z are all integers; x is the case number (starting from 1), y is the sum of N and M, and z is the product of N and M.

// input.txt
3
1 5
7 -2
9001 0

That’s it.

You aren’t given a template function. There isn’t a defined input object. You have a blank page and you just need to copy-paste the code that solves the problem on the page.

So how do you get started?

Standard input and output

For those using Node JS, I recommend using Node’s readline.

You can define the standard input and output as follows:

const readline = require('readline')
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  terminal: false
})

readline has the following events.

  • close
  • line
  • pause
  • resume
  • SIGCONT
  • SIGINT
  • SIGTSTP

The ones I used are line and close.

The basic idea is this:

rl.on('line', function (line) {
    // process all of the given input, line by line
  })

  .on('close', () => {
    // finished processing the input
    // now we can solve the problem
  }

Let’s go back to the given problem.

Consider the following very simple problem. The input consists of one line with a number of test cases T, and then T more lines, each of which contains two positive integers N and M. The desired output form is Case #x: y z, where x, y, and z are all integers; x is the case number (starting from 1), y is the sum of N and M, and z is the product of N and M.

// input.txt
3
1 5
7 -2
9001 0

We could process the input as follows:

let problem = {
  T: 0,
  testCases: []
}

rl.on('line', function (line) {
  // Process input
  if (problem.T === 0) {
    problem.T = Number(line)
  } else {
    const [a, b] = line.split(' ')
    const aNum = Number(a)
    const bNum = Number(b)

    problem.testCases.push([aNum, bNum])
  }
})

  .on('close', () => {
    // Finished processing input, now solve question
    solveProblem(problem)
    process.exit()
  })

Given the simplicity of the question, it may be easier to just solve the entire problem as you read in the input inside the line event. But actual Code Jam questions are much more complicated, so I found it helpful to isolate the logic of reading the input from the problem-solving code.

Testing it locally

Put both your JS code and input.txt in the same folder.

Your directory structure should look something like this:

app.js
input.txt

If so, you can simply run node app.js < input.txt on your terminal and your code will consume all the test cases inside input.txt.


Hope this helps! Happy programming!