# How to solve programming exercises

Coding bootcamps require students to solve self-contained programming exercises (‘toy problems’) that grow in difficulty until they are representative of software engineering interview problems.

I struggled significantly with these exercises until I learned about the **OICEPIDR** (Mnemonic: “Oh, an ice peddler!”) approach illustrated below.

**OICEPIDR Rules**

- Approach each step one after the other, not in parallel.
- Write or type the responses to each step in your editor. Do not rely on your memory.
- Do not write any code until the
**Implementation**phase.

### Problem Statement

Write a function (sumTree) that adds up the values of all nodes of a two level tree and returns the sum in linear time. Do not use the built in reduce method. The addChildren methods are provided.

var root = new Tree(5);

root.addChildren(2);

root.addChildren(9);

console.log(root);

//{value: 5, children: [{value:2, children: []}, {value: 9, children: []}]}

#### Output

- Determine the output’s type (string, number, array, object, 2D array, etc.)

The output type is a number.

#### Input

- Determine the input’s type (string, number, array, object, tree, etc.)

The input type is a node: an object with two properties:

- Value, which is assigned to a number.
- Children, which is assigned to an array of nodes.

#### Constraints

- Are there any time complexity constraints?

Yes - If so, what are they?

The function must run in linear time - Are there any space complexity constraints?

No. - If so, what are they?

N/A - Are any techniques (built in methods, etc.) not allowed?

The built-in reduce method for Arrays is not allowed.

#### Example test cases

var root = new Tree(5);

root.addChildren(2);

root.addChildren(9);

console.log(sumTree(root)); //should evaluate to 16

- Generate at least one test case that maps input to output. See the above as an example simplified test case.
- Check the example input and output pairs against the problem statement’s provided examples, your best guess if no examples are provided, or with your interviewer if this is an supervised exercise.
- Put a comment next to each input’s log statement with the example output.
- Brainstorm other test cases that could break the implementation.
- Add these test cases using the same process as above.

#### Pseudocode

- Write how you would solve the problem as if explaining it to a friend.

Since we want the sum, we will need to create a place to store it. After that, we’ll need to access the value property for the first node, and then add the value properties for its children nodes to the sum.

2. Outline the pseudo code in the same indentations as actual code.

//define the function sumTree that takes a node parameter

//set sum equal to an initial value of zero

//add the value property of the parent node to sum

//iterate over each child node

//add the value of each child to sum

//return sum

#### Implementation

- Convert the pseudocode to your target language (Javascript in this case).
- Run the code.

//define the function sumTree that takes a node parameter

//set sum equal to an initial value of zero

//add the value property of the parent node to sum

//iterate over each child node

//add the value of each child to sum

//return sum

function sumTree(node) {

var sum = 0;

sum = sum + node.value;

for(i = 0; i < node.children.length; i++) {

sum = sum + node.children[i];

}

return sum;

}

//test cases below

var root = new Tree(5);

root.addChildren(2);

root.addChildren(9);

console.log(sumTree(root)); //should evaluate to 16

#### Debugging

- Compare the output against the test cases.

The output does not seem to match the first test case. - Mentally walk through the evaluation as if you were the interpreter.

When passing in root, we find that sum + node.value = 5, so no issue there.

However, inside our for loop, we are attempting to add each child node’s object, not value, to sum. - Implement the correction and repeat from 1 for other failing test cases

//define the function sumTree that takes a node parameter

//set sum equal to an initial value of zero

//add the value property of the parent node to sum

//iterate over each child node

//add the value of each child to sum

//return sum

function sumTree(node) {

var sum = 0;

sum = sum + node.value;

for(i = 0; i < node.children.length; i++) {

sum = sum + node.children[i].value;

}

return sum;

}

//test cases below

var root = new Tree(5);

root.addChildren(2);

root.addChildren(9);

console.log(sumTree(root)); //should evaluate to 16

#### Refactor

- Reread the code while brainstorming legibility improvements

1) The for loop can be changed to a forEach loop for shorter code.

2) Sum can be initialized with the initial node’s value. - Implement the changes.
- Check if all tests continue to pass and debug if they do not.

//define the function sumTree that takes a node parameter

//set sum equal to an initial value of zero

//add the value property of the parent node to sum

//iterate over each child node

//add the value of each child to sum

//return sum

function sumTree(node) {

var sum = node.value;

node.children.forEach(function(child){

sum = sum + child.value;

}

return sum;

}

//test cases below

root.addChildren(2);

root.addChildren(9);

console.log(sumTree(root)); //should evaluate to 16