C++

Lattice Paths Part 2 (Brute Force Modeling and Iteration)

Starting in the top left corner of a 2 x 2 grid, and only being able to move to the right and down, there are exactly 6 routes to the bottom right corner.

How many such routes are there through a 20 x 20 grid, or any N x N grid?

--

This is the second of a three-part series article about explaining how you could have arrived at the solution yourself in solving the Lattice Paths Problem. If you haven't tried it yet, do it first. I do not intend to deny you of your epiphany moment!

Note: All implementations are written in C/C++.

--

Lattice Paths Part 1 (Brute Force)

Starting in the top left corner of a 2 x 2 grid, and only being able to move to the right and down, there are exactly 6 routes to the bottom right corner.

How many such routes are there through a 20 x 20 grid, or any N x N grid?

--

This is the first of a three-part series article about explaining how you could have arrived at the solution yourself in solving the Lattice Paths Problem. If you haven't tried it yet, do it first. I do not intend to deny you of your epiphany moment!

Note: All implementations are written in C/C++.

--

How to Generate a Square Star Fractal Using Recursion

This article is about generating a Square Star Fractal using recursion (Although a triple for-loop implementation is much preferred, it will be discussed separately).

The logic of a Recursive Square Star Fractal Function is a simple Divide and Conquer algorithm. The program is subdivided into smaller tasks until it is solved. The function takes the (X, Y) middle coordinates of the square, R length, where 1 Side of Square = 1 Length = 2R length, and T as the number of times to generate 2R- sized squares. 

Tags:

Techniques How to Remove Any Recursion

A function that calls itself is hard to visualize for most developers. Common software requirements are implemented thru loops, arrays, variables, and automated function calls thus making the use of recursion and variations of the list data structure unusual. In this article, we will take a look at how we can remove any recursive program by following the simple techniques described below. It is very important to understand that this article is not saying that recursion is bad.

How to Check for Matching Parenthesis

Another common whiteboarding question you will be asked is to create a function where you need to check a given expression if all open parentheses have matching close parentheses or vice versa.

An example of expression is: {{[([({{(([]){})){}((((((]}]}}

Evaluating this expression will result to false. 

Arrays

An array is one of the fundamental data structures available and most widely used by developers today to solve general problems. You can think of an array as a collection of variables of a certain data type stored contiguously in memory. The values of an array can be accessed through an index (commonly represented by the variables i, j, and k), and the ith value of an array is denoted as Array[i].

This article is not to be confused with associative arrays, which is a fancy way of calling the key-value-pair data structure. We will discuss it separately.

How to Swap Two Variables Without a Third Variable?

One of the most common whiteboarding questions is "Swapping of two (2) integers without using a third variable". The objective is not to optimize the standard way of swapping variables but rather to show the resourcefulness of the applicant given problems with constraints. Because in real life, you'll face challenges where you have to work on projects with limited resources.

But how do we approach this problem? The computer operations available are:

How to Find the Larger Number Without Conditional Operators Part 2 (Arithmetic)

Given two (2) signed integer numbers, A and B. Find the larger number between these two (2) variables, using only arithmetic expressions. Provide a solution to the following:

  1. Assign 0 to X, if A = B, or assign the larger number between A and B
  2. Assign the larger number between A and B to Y, or assign either A or B, if A = B

How to Find the Larger Number Without Conditional Operators Part 1 (Using Bitwise Operators)

Given two (2) signed integer numbers, A and B, where:

  • size = ( Integer size int = 4 bytes* = 32 bits ) - 1
  • -2size ≤ A ≤ 2size-1, and
  • -2size ≤ B ≤ 2size-1

Find the larger number between variables A and B, without using conditional operators. Provide a solution to the following:

  1. Assign 0 to X, if A = B, or assign the larger number between A and B
  2. Assign the larger number between A and B to Y, or assign either A or B, if A = B
Subscribe to RSS - C++