Nutty Efficiency: What Pistachios Teach About Data Structures & Algorithms

A whimsical story about a nutty encounter with a manager and how we can use pistachios to build efficient data structures and algorithms.

Chuma S. Okoro
5 min readDec 19, 2024

What happened?

One of the main reasons I started working at Bloomberg was for the pistachios. Not only are they delicious, but they have the perfect amount of salt. Last but not least, the nuts are uncracked when we get them from the pantry. This allows us to put some effort in and reap the spoils of our labor when we get the nut out. It’s scientifically proven that the pistachio nut is tastier if you’ve worked to get it out.

Clearly, the management at Bloomberg feels similarly because, in an informal coffee chat last week, I saw a manager grab a cup full of pistachio nuts for the meeting. While we were chatting, I couldn’t help but notice the manager’s system for getting the pistachio nut out of the shell. They had a single cup to store both the pistachios with nuts in them and the empty cracked shells after they got the nuts out.

For the first few minutes, this process was easy because a large percentage of the shells were uncracked(meaning they still had a nut inside). The longer the meeting went, the more the shells dominated the nuts. Around minute ten of the meeting, the manager was having tremendous difficulty finding the next nut. They had to sift through the entire cup to find a pistachio that was uncracked. What made it worse was that the cup basically got scrambled every time the manager dipped their hand in the cup.

What’s the problem?

The “big back, big back” song had to have been playing in my head because this process was infuriating me. When I’m eating pistachios, it’s because I’m feeling a bit peckish and I certainly don’t want to be wasting time searching for a nut. So of course, I had to say something and offer a solution

First, let’s break the problem down. The storage mechanism the manager chose was a single cup. We can represent this as the map data structure where the key is a random ID for a nut and the value is the cracked status. The main operation we run is crackNut. In cracking a nut, we need to retrieve a nut from storage, eat the pistachio, and throw away the shell.

In the software space, we tend to represent operations in terms of the worst-case scenario. This is because we often build systems that need massive scale. So if we apply this to the pistachio problem, there’s no issue if you’re only eating 5 pistachios. But what about 15? 40? 80? The more pistachios we go through, the worse it gets.

In the worst situation where there is only one nut left, we have to go through every shell to find one that is not cracked. We can represent the time complexity of this as O(n) where n is the amount of pistachios we have. If we are aiming to crack every nut, the time complexity would be O(n^2). For real pistachio nut enthusiasts, this is untenable. But don’t fret, I have a solution.

How can we solve this?

I told the manager that I was a professional pistachio nutcracker and I knew how to make his process more efficient. The first thing we need to do is introduce a separate storage device for the cracked shells. By separating the cracked shells from uncracked ones, we incur virtually no extra space.

The main benefit of this additional storage is apparent when we look at how the crackNut operation would work. With separate data stores for cracked and uncracked nuts, it’s way easier to get an uncracked nut. Every time I want one, I can take any nut from the uncracked nuts data stores. Even in the worst-case scenario, the time complexity would be O(1). If I aim to crack every nut, my time complexity would be O(n).

When I shared my pistachio-cracking secret, the manager grinned from cheek to cheek.

Visualize this!

Lucky for you, I built a little web application to demonstrate the problem and solution for you. If you go to this free web app called Nutty Efficiency, you can see a fun site where you can mimic the two scenarios.

Enter a number for the pistachios and hit Initialize. The two cups will be filled with uncracked pistachios. When you hit the Simulate button, the program will start cracking the pistachios with the Pacman eating the nuts. You should observe that “Scenario 2” on the right will crack nuts more quickly than “Scenario 1” on the left.

Thanks for reading! This repo is open source so if you want to contribute, check out the codebase and create a pull request with the enhancements you think would be useful.

--

--

Chuma S. Okoro
Chuma S. Okoro

Written by Chuma S. Okoro

Sr. Software Engineer @ Bloomberg. I love talking humor, tech, and business. Every article is my opinion and thoughts. I don't speak for Bloomberg

No responses yet