To watch the youtube video going over this topic, please click here.
Problem Statement
Given an array, for each element find the next greater element (to the element's right). If there is no greater element then the value is 1.
Approach 1: O(N^2) Time  O(1) Space  For Each Element, Look Right
The naive approach is pretty simple and uses nested loops. For each element of the array, look to the elements to its right. If we find a greater element, cool. If not (and we've looked at every element to the right), then the next greater element is 1.
Here's some pseudocode that demonstrates this algorithm.
The time complexity is O(N^2) since every element potentially has to visit every element to its right.
The space complexity is O(1) since we don't use any extra space. Approach 2 (Best): O(N) Time  O(N) Space  Use A Stack
One way to solve the problem is with a stack.
The stack stores the index of elements that don't have their NGEs yet. Every time we visit an element, we check if is value is greater than the element on top of the stack. The following picture shows the setup we need to use a stack to find the NGE of each element in the array. We'll go through that particular example in the video. (So watch the video if you want to see how it works.)
The following pseudocode demonstrates the algorithm.
The time complexity is O(N) since we visit each element only once.
The space complexity is O(N) since at worst we have to put all elements onto the stack.
The source code of a working implementation (written in C++) can be found on github here.
Optimizations
There are possibly optimizations, regardless of the algorithm chosen:
Edge Cases
Edge cases to cover:
Like this content and want more? Feel free to look around and find another blog post that interests you. You can also contact me through one of the various social media channels.
Twitter: @srcmake Discord: srcmake#3644 Youtube: srcmake Twitch: www.twitch.tv/srcmake Github: srcmake
Resources
1. https://www.geeksforgeeks.org/nextgreaterelement/ To watch the video that goes over this topic (which also visualizes the example section), click here. What Are Bloom Filters?A Bloom Filter is a data structure that uses multiple hash functions on a key to mark values in a boolean array. The point of a bloom filter is to store whether a key was visited before. An example usecase for a Bloom Filter might be a web crawler, which needs to store whether we've visited a website before. Bloom Filters are NOT about keyvalue pairs like Hash Maps. There's no "value" associated with a key. The only thing we care about for a key is a boolean: true that we've seen this key before, or false that this key is new. Bloom filters are good because they use less memory than a hash map and still provide fast lookup and insertion times. The reason bloom filters aren't incredibly common is because it's possible they give wrong answers: a Bloom Filter might say that you've visited a key before when you really haven't. We'll see this in the upcoming example section. How Bloom Filters WorkA bloom filter can be represented as a list of k hash functions and an array of booleans that starts off with all values initialized to false. A hash function is simply a function that takes some input, and transforms that into some number (which we'll use as the index in our array) as output. The same input will always produce the same output. We care about two operations in a bloom filter:
Insertion works by inputting a key into all k hash functions, and for each outputted array index, marking that array value as true. Querying works by inputting a key into all k hash functions, and for each outputted array index, checking whether that array value is true. If all array values are true, then the key was seen before. If any array values are false, then the key wasn't seen before. The time complexity of inserting and querying are both O(k). Bloom Filter ExampleLet's say we have a bloom filter with an array initialized to false (and pretend our array can hold negative numbers), and let's say k=3 so that we have 3 hash functions (which takes a string s as input, for simplicity). The following operations show the various interactions with a bloom filter (I'll show these in the video):
ConclusionBloom Filters are important data structures and they're frequently asked about in interviews. They're good because they're efficient on space, but the drawback is that they produce false positives, possibly saying it's seen a key before when it really hasn't. We've only looked at a basic bloom filter example to get the concept behind them, because optimizing a real bloom filter takes a lot of tuning to decide a good k and depends on the amount of memory you have. And of course, we saw no code today because the basic concept of a bloom filter is easy enough to code, but coding a real one is a bit involved (because of the complex nature of making good hash functions). Keep this data structure in mind during interviews and bookmark this page if you ever need a refresher. Like this content and want more? Feel free to look around and find another blog post that interests you. You can also contact me through one of the various social media channels.
Twitter: @srcmake Discord: srcmake#3644 Youtube: srcmake Twitch: www.twitch.tv/srcmake Github: srcmake
To see the youtube video where I draw out how each algorithm works (recommended), please click here.
Problem Statement
Given an array of integers, move all zeroes to the end of the array. Ideally, use no extra space, and keep the nonzero elements stable (meaning the order is preserved).
Attempt #1: Keep Moving Zeroes To The Right  Stable  O(N^2) Time  O(1) Space
A simple algorithm is to go through the array from left to right, and every time you encounter a zero, you search the rest of the array for a nonzero to swap with. However, this approach has a bad Time complexity O(N^2) so it's not good.
Here's some pseudocode showing how you'd implement this:
Stable: Yes
Time: O(N^2) Space: O(1) Attempt #2: Place Elements Into New Array  Stable  O(N) Time  O(N) Space
An easy approach to this question is to create a new array, and place all nonzero elements into it. Then fill the remainder of the new array with zeroes. This approach takes O(N) extra space, so it's not ideal.
Here's some pseudocode showing how you'd implement this:
Stable: Yes
Time: O(N) Space: O(N) Attempt #3 (Best): Special Index Tracking  Stable  O(N) Time  O(1) Space
The ideal solution is a bit tricky. The idea is to go through the array and move all nonzero elements to the left. Where on the left do we write each nonzero element too? We'll keep track of that index basically by counting how many nonzeroes we saw so far. This is the ideal solution because the Time complexity is O(N) and the Space complexity is O(1).
Here's some C++ code for how you'd implement this:
Stable: Yes
Time: O(N) Space: O(1) Edge Cases
The complete code for this project can be found on my algorithm questions and answers repo on github here.
Like this content and want more? Feel free to look around and find another blog post that interests you. You can also contact me through one of the various social media channels.
Twitter: @srcmake Discord: srcmake#3644 Youtube: srcmake Twitch: www.twitch.tv/srcmake Github: srcmake 
AuthorHi, I'm srcmake. I play video games and develop software. Protip: Click the "DIRECTORY" button in the menu to find a list of blog posts.
License: All code and instructions are provided under the MIT License.
