A few months back, I entered the crazy world of LeetCode.
For those of you who don’t know, LeetCode is a site that posts thousands of coding problems at varying difficulties, and asks you to solve them, whilst providing various test cases that your solution must pass. Additionally (and in my opinion most valuably), LeetCode also has a big community of users who share and comment on solutions to said problems.
LeetCode’s most common user is someone seeking employment at one of the world’s biggest tech companies, all of whom utilise coding problems (AKA algorithmic coding) as a means of…
In the world of programming, there are many ways to sort data. For the average engineer, you might just use
.sort(), perhaps with a slightly modified sorting function to return your data in a specific way But that’s probably about the most you do.
If however you’ve ever taken an algorithms class, used LeetCode, or just dived down a little deeper into sorting performance of a platform, you’ve probably heard of sorting algorithms.
Now, make no mistake, your favourite language’s
.sort() (or equivalent) already uses one of these algorithms. In fact, if we’re being honest, it probably uses it in…
In this LeetCode problem, we’re given an array of numbers, and a target. Our job is to come up with every possible unique way to make that target by adding the numbers in the array. We can use the numbers multiple times, and the arrangement is considered unique if the quantity of at least one number is different.
For example, given the array of numbers
[2, 5, 6] and a target of 12, the solutions include
[2, 2, 2, 2, 2, 2],
[2, 2, 2, 6] ,
[2, 5, 5] and
In this LeetCode problem, we’re asked to delete a node from a Linked List. However, we aren’t given the Linked List’s head, and are instead provided with the node that needs to be deleted.
To be honest, this question is a little too simple. Thanks to the question guaranteeing that you won’t be given the tail node of a Linked List, there aren’t even any edge cases to handle, so the solution comes in at a whopping 2 lines.
This incredibly simple solution simply replace the to-be-deleted node’s value with the next one in the Linked List, as well as the
next value also. This technically deletes the next node in the list after copying its contents, but in doing so achieves the original goal of deleting the supplied node.
In this LeetCode problem, we’re given a Linked List, and asked to determine whether or not it (or rather, its values) represents a palindrome.
Revision point 1: A linked list is a data structure that consists of two properties;
next which points to the next node in the list, and
val which tells you the current node’s value.
Revision point 2: A palindrome is something that is the same when reversed. For example, “dad” is a palindrome, as is “12321”, but “test” is not, and nor is “123”.
What we’re being asked here then, is to get the values from…
In this LeetCode problem, we’re given a string str and an integer k, and asked to return the longest substring of str, which is made up only of characters that appear k or more times.
In other words, given the string ‘aabbbcccc’ and a k value of 3, the longest substring where all letters appear 3 or more times would be ‘bbbcccc’, so our answer would be 7.
The most obvious brute force solution is to construct every possible substring, and then check if it is made up entirely of characters that appear k or more times. To do this…
In this LeetCode problem, we’re given what is arguably the best known programming challenge out there: FizzBuzz. For those few of you who have never heard of such a thing, the idea is to create a function that will count from 1 to N (N being a number provided to the function), outputting the number as it goes. However, if it encounters a multiple of 3, it will output “Fizz”, if it outputs a multiple of 5, it will output “Buzz”, and if it encounters a multiple of both 3 and 5, it will output “FizzBuzz”.
Note that many of…
In this LeetCode problem, we’re given an array of numbers and asked to find the number of combinations — made by taking 1 number from each array — that when added together total 0.
In other words, when given the following arrays:
A: [2, 6]
B: [1, 2]
C: [-1, 9]
D: [-2, 7]
The answer would be 1, as the only combination would be 2 from A, 1 from B, -1 from C and -2 from D, all of which add together to make 0.
The obvious brute force solution to this problem is to run 4 for loops…
Quick sorting is a sorting algorithm that focuses on placing a single value of an array in its correct position for each iteration. It does this by splitting the array at a pivotal point (called the pivot), and then moving all numbers greater than that pivot to be after it, and all numbers less than the pivot to be before it.
For example, given the array
[3, 7, 4, 5, 9], you might select a pivot point of index 3 (which has a value of 4 in the above array). You would then go through each number and ask “Is…
Sorting data is a notoriously slow process. In real life, if you’re given a list of 10 numbers and asked to sort it, you’ll probably first look through all the numbers for the lowest one, then you’ll look through them again for the next lowest, and you’ll repeat this process until you’ve covered everything.
A lot of the time in code, it’s a similar story. You’ll loop through the numbers, and then for each number, you’ll compare it with all of the other numbers. Slow and inefficient.
Enter “merge sort”, a concept where we split our to-be-sorted data in two…
Full-stack software developer from the UK, author of the Aftermath book series, full time tech-nerd.