Back to Data Structures and Algorithms Interview
Data Structures and Algorithms Interview

Data Structures and Algorithms Interview

0 of 20 Completed

N-Dimensional Arrays

So far, the arrays that we have discussed are under the domain of one-dimensional arrays. Technically speaking, we use a single index to access an element inside an array.

my_list = [1, 2, 3, 4, 5] 
my_list[1] # access the second element

# or using our custom array class
array = Array()
array.emplace_back(3)
array[0] # access the first element

However, arbitrarily, arrays can hold any type of element. Depending on the programming language, a single instance can hold one of any type (example: an array holding all integers), or can hold any type, regardless of the other elements (example: an array holding a string, an integer, and a custom user object). Nevertheless, at the very least, an array can hold any type. That means that arrays can hold other arrays too!

ndArray = Array(Array())  # ndArray[0] is another array.
ndList = [[]] # ndList[0] is another list.

Arrays that contain other arrays are called n-dimensional arrays. The n in n-dimensional stands for the number of dimensions an array has, but for the sake of simplicity, we can refer to it as the number of indexes it takes to access a raw element inside an array.

array1 = [1, 2, 3]
one = array1[0] # one-dimensional
array2 = [[1, 2], [3, 4]] 
two = array2[0][1] # two-dimensional
array3 = [[[1, 2]],[[3, 4]]]
three = array3[1][0][1] # three-dimensional

Python’s lists typically do not care for the types of its elements, and therefore, have no intrinsic dimension. For example, we can have an array that looks like this:

array = [1, [2, 3], 4] 
one = array[0]
two = array[1][0]
three = array[1][1]
four = array[2]

As you can see, the array / list called array can have its raw elements accessed using both one and two indexes, depending on the element pointed to at the current index. In this case, Python lists have no clearly-defined dimension. ****

However, for the sake of simplicity (and platform agnostic-ness) let’s assume that arrays must have all of its elements to be the same type. In this case, arrays like these have well-defined dimensions. All elements must be accessible within n indexes.

matrix_array = [[1, 2, 3],
								[4, 5, 6],
								[7, 8, 9]]
# Here, we can clearly define the shape of the `matrix_array` to be 3x3,
# it being a three-dimensional array.

Indexing With N-Dimensional Arrays

When handling multiple indexes, indexing can be a bit wacky. Because we are handling multiple indexing at a time, it might get a bit confusing. But here is the general gist: n-dimensional indexing follows what we call as a row-major order. Let’s use 2-dimensional arrays for simplicity.

As a rule of thumb, the leftmost index refers to the the array with a lower dimension. For example, given the array [[1, 2],[3,4]], the lower, or the first dimension, is the bigger array encompassing the inner arrays [1, 2], and [3, 4]. So when we say array[1], we are referring to the second internal array. So, array[1] == [3, 4]. You can continue with this logic using n-dimensional arrays.

Challenge Question: N-Dimensional Indexing

We are given an array array = [[[1, 2],[3, 4]],[[5, 6],[7,8]]]. How can I access the number 7?

A. array[1][1][0]

B. array[1][0][1]

C. array[0][1][1]

D. array[0][0][2]

Now that you know how to index with n-dimensional arrays, can you solve problems using them?

0%

Completed

You have 20 sections remaining on this learning path.