After a break, I started teaching myself more of Python by taking on data structure and algorithms questions on Leetcode. The reverse an integer one is one I thought would be simple enough to dive back in.

I chose to write about this question because, while simple, there are many ways to do it. However, most solutions I’ve seen are needlessly complex. Further, they’re hard to read and interpret what’s going on. Thus, not great for learning. …

A Step-by-Step Guide with Code Walk Through

In this Leetcode problem, we’re told that we’re given an ordered array of positive integers that has some missing numbers as input, along with a number (k). Our task: find the kth missing number in that input array. Sounds more complicated than it is. Let’s get at it.

Let’s say we have an array with the number [1, 2, 3, 4], and we’re told to find the 2nd missing number. Confusing. Doesn’t seem like there is a missing number in that array.

We’d have to assume that the two missing number are 5 and 6, and thus, the 2nd missing number is 6, and the full array would actually be [1, 2, 3, 4, 5, 6]. …

Remember those edge cases!

While this problem might not be too difficult at first glance, I believe the main lesson of it is dealing with edge cases. The problem states that we’ll get a string of upper/lower case alphabet characters, along with white spaces. All we have to do is return a number that tells us how long the last word of that string is.

We don’t necessarily know how long this string will be. It could be one word. It could be a whole paragraph. It can even be empty. Thus, I think this is mainly an exercise of dealing with non-happy-path inputs. …

Applying separation of concerns concepts and backtrack recursion

After bootcamp graduation I spent a lot of time learning CS fundamentals, data structures and algorithms. And while I have seen and solved problems using recursion before (hello binary search tree traversals!) this was the first time I encountered a problem where immediately I thought: while loop or recursion. But recursion is where I need most practice, so let’s do that!

The problem states that we’re going to be given a number, say 38. Our goal is take this number apart (3 || 8), add the digits (3 + 8 = 11), and to keep doing this until our number is a single digit, and then return it. At this point we’d need to do it one more time: 1 + 1 = 2. …

I’m a visual learner and thinker. So let’s tackle this leetcode problem in a non-esoteric way.

Pretty simple when it’s not in mathy/code speak: we’re given one number (n) and another number (k). We have to return the kth factor of n. If there isn’t the kth factor, we should return -1.

First of all, let’s remind ourselves what a factor is: a number that divides into another number exactly and without leaving a remainder.

Let’s say we’re given a number: 12. What are all its factors?

`factors: 1, 2, 3, 4, 6, 12`

Because if we divide 12 by any of these numbers, we’d have 0 as a remainder. …

I was recently solving a problem, where one of the sub problems to solve was finding the second largest number in an array of integers. I’m humble enough to admit it: I froze for a bit. I know the typical methods for moving/returning at the start and end we use all the time (pop, shift, unshift, etc). But what about the element that’s 2nd to last?

Let’s walk through a few scenarios with increasing complexity.

If you know the array is already sorted, getting the second largest number is simple:

const arr = [1, 2, 3];function findSecondLargest(arr) {

return arr[arr.length …

A guide to solving LeetCode #46: Permutations

If you’re reading this, I reckon that, like me, you were having difficulty solving this LeetCode problem. Or, also like me, you want to lean into understanding recursion better. This is the post for you. We’ll go through my step-by-step solution on how to solve this problem with recursion.

Given a collection of **distinct** integers, return all possible permutations.

**Example:**

**Input:** [1,2,3]

**Output:**

[

[1,2,3],

[1,3,2],

[2,1,3],

[2,3,1],

[3,1,2],

[3,2,1]

]

First, I believe it’s a good habit to solve these kinds of problems manually, like a typical person would intuit solving it. And in the process, note the steps you take (i.e. pseudocode). …

Tackling permutations and recursion one step at a time.

Solving a permutation problem with recursion has been particularly difficult for me to wrap my head around. It just doesn’t seem as natural as iteration. So let’s take a gentle journey, you and me. We’ll walk through a recursive solution together. Step-by-step.

This is my dog Lula. Dog’s are comforting. So let’s think of cute fluffy pups while thinking of scary weird algorithms.

Lula’s a sweetie. A smartie pants. A loud barker. Let’s permute Lula. Or…her species. Or…the word “dog”. …

I failed the interview question. Let’s make sure we both learn a lesson.

I recently had a tech interview where I was asked this question:

*Given an array of characters return a string that counts the number of each consecutive letter.*

*Example: [“a”, “b”, “b”, “a”] should return “a1b2a1”*

Seems simple enough. I’m sure a nested for loop…

Maybe it was nerves. But I simply couldn’t figure it out in the allotted 20 minutes. After licking my wounds, I got back up on that horse, and looked at it with fresh eyes with the help of a friend. …

The Romans could build, but boy were their numbers complicated!

A buddy of mine recently had a technical interview where he was asked to build a function that would take a Roman numeral, and return a regular integer most of us can read and understand. I thought that might be useful anytime reading the credits to movies filmed prior to 1990.

The thing about Roman numerals, though, is that they’re not all that intuitive. For example, if 1 = I, and 2 = II, and 3 = III, you might expect 4 to equal IIII. But of course it doesn’t. It’s IV. So I immediately thought, “oh…I’m not sure I know all these rules.” …

About