Algorithms, Session 2, Data Structures

v1.0.0
2021-10-15

Arrays

Get product of all other elements

Given an array of integers, return a new array such that each element at index $i$ of the new array is the product of all the numbers in the originial array except the one at $i$. For example, if our input was $[1,2,3,4,5]$ the expected output would be $[120,60,40,30,24]$. If our input was $[3,2,1]$, the expected output would be $[2,3,6]$.

Follow-up: what if you can't use division?

Locate smallest window to be sorted

Given an array of intergers that are out of order, determine the bounds of the smallest window that must be sorted in order for the entire array to be sorted. For example given $[3,7,5,6,9]$, you should return $(1,3)$.

Linked lists

Reverse linked list

Given the head of singly linked list, reverse it in place.

Add two linked lists that represent numbers

We can represent an integer in a linked list format by having each node represent a digit in the number. The nodes are connected in reverse order, such that the number $54321$ is represented by the following linked list: $5 \leftarrow 4 \leftarrow 3 \leftarrow 2 \leftarrow 1$. Given two linked lists in this format, return their sum. For example, given $9 \rightarrow 9$ and $5 \rightarrow 2$ you should return $124$ $(99+25)$ as $4 \rightarrow 2 \rightarrow 1$.

Rearrange a linked list to alternate high-low

Given a linked list, rearrange the node values such that they appear in alternating $low \rightarrow high \rightarrow low \rightarrow high \rightarrow ...$ form. For example, given $1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$ you should return $1 \rightarrow 3 \rightarrow 2 \rightarrow 5 \rightarrow 4$.

Find intersecting nodes of linked list

Given two singly linked lists that intersect at some point, found the intersecting node. Assume the lists are non-cyclical. For example, given $A = 3 \rightarrow 7 \rightarrow 8 \rightarrow 10$ and B = $99 \rightarrow 1 \rightarrow 8 \rightarrow 10$, return the node with value $8$. In this example, assume nodes with the same value are the exact same node objects. Do this in $O(m+n)$ time (where $m$ and $n$ are the length of the lists) and constant space.


Stacks and queues

Implement a max stack

Implement a stack that has the following methods:

Each method should run in constant time.

Determine whether brackets are balanced

Given a string of round, curly, and square opening and closing brackets, return whether the brackets are balanced (well-formed). For example, given the string "( [ ] ) [ ] ( { } )", you should return True. Given the string "( [ ) ]" or "( ( ( )" you should return False.

Similar problem: aoc 2017 day 9

Reconstruct array using +/- signs

The sequence $[0,1,...,N]$ has been jumbled, and the only clue you have for its order is an array representing whether each number is larger or smaller than the last. Given this information, reconstruct an array that is consistent with it. For example, given $[\text{None}, +,+,-,+]$ you should return $[0,1,3,2,4]$.


Trees

Count unival trees

A unival tree (which stands for "universal value") is a tree where all nodes under it have the same value. Given the root to a binary tree, count the number of unival subtrees. For example, the following tree has 5 unival subtrees:

graph TB A((0))-->B((1)) A-->C((0)) C-->D((1)) C-->E((0)) D-->F((1)) D-->G((1))

Evaluate arithmetic tree

Suppose an arithmetic expression is given as a binary tree. Each leaf is an integer and each internal node is one of +,-,* or /. Given the root to such a node, write a function to evaluate it. For example, given the following tree:

graph TB A((*))-->B((+)) A-->C((+)) B-->D((3)) B-->E((2)) C-->F((4)) C-->G((5))

You should return $45$ as it is $(3+2) * (4+5)$

Get tree level with minimum sum

Given a binary tree, return the level of the tree that has the minimum sum. The level of a node is defined as the number of connections required to get to the root, with the root having level zero. For example:

graph TB A((1))-->B((2)) A-->C((3)) C-->D((4)) C-->E((5))

In this tree, level $0$ has sum $1$, level $1$ has sum $5$ and level $2$ has sum $9$, so the level with the minimum sum is $0$.