Deciphering JS Bundles, Python’s Immortality, and the Knapsack Conundrum
🎬 S1:E05 Bugs, Bundles, Brainwork
"Complexity is your enemy. Any fool can make something complicated. It is hard to make something simple." – Richard Branson.
Hey, TechFlixers!
I took a short break last week, but we are back with a brand-new episode of Techflix Weekly with subtle changes in the format.
Let’s kick off with fun trivia. Did you know that the first computer bug was a real insect? Back in 1947, when computers were the size of entire rooms, a moth got trapped in a relay and caused a fault. It was later pinned and preserved as the “first computer bug.”
🔦 Spotlight
How to reduce your JS bundle size?
First, let’s understand why?
Faster load times.
Better SEO ranking.
Less data/memory.
Increased conversions.
Cost saving.
A real-world case study: How Dropbox reduced its bundle size by 33%.
TL;DR: They started using Rollup as their module bundler, taking advantage of its features like automatic code splitting (loading code chunks as required) and tree shaking (removing unused code).
What are immortal objects in Python?
Instagram introduced a new feature in Python as part of PEP-683 to optimize the processes of their front-end server (built with Django).
To speed things up, they use a special setup where they pre-load and share common data among different asyncio processes, making sure they only read it, not change it.
But they noticed that the shared data decreased over time, while each process memory increased. Even though their data didn't change, the way Python keeps track of these data pieces was changing them slightly. This was due to the way Python cleans up unused data (garbage collection) and counts references to objects.
To solve this problem, they came up with a solution called “Immortal Objects,” which are data pieces that don't change (even with metadata, and hence truly immutable). This lets Python know which data pieces can be touched and which can't, via a special marker they are created with.
Through immortal objects, Instagram greatly reduced private memory by increasing shared memory usage.
☕️ Tea Time
Tech companies are coming to India! Here are some firms expanding their India operations this year- Okta, Databricks, Disco, Continental, and you guessed right, they are hiring! Click on the hyperlinks to check the type of open roles at each firm. Even firms like Adobe are expanding their India development centers—Some great growth opportunities ahead.
Last week, I tested the code interpreter plugin that comes with the ChatGPT Plus subscription. I made it do a lot of stuff, from analyzing Excel sheets to creating posters and even reviewing my LinkedIn profile. TL;DR: It does it all pretty well, but there is a lot of scope for fine-tuning the prompts to get better results. Find out the detailed results here.
🚀 Power Up
This week, we explore an important concept tested frequently in data structures and algorithms interviews. As always, our goal is to build INTUTION to figure things out.
The Knapsack Problem
It can be presented in many different types of coding problems, but all of them boil down to a similar pattern.
You have a bag with a weight limit.
You have items, each with a weight and value.
Aim: Fill the bag to get the most value without going over the weight limit.
Traditional approach
Try every combination of items and find the one with the max value.
Lots of calculations: For N items, 2^N combinations.
What if you don’t have to find all the combinations? There’s a smart way to think about it.
Imagine if you have only one item. Ask yourself: "Should I take it or not?". The answer is your direct solution.
Let’s say, we have two items. Let’s call this “Step 1”. You have a chain of choices:
Take item 2.
Update leftover weight in the bag (W - W2).
Take or leave item 1, for the new weight limit.
Leave item 2.
The weight limit is the same.
Take item 1.
You can rephrase the above process— “Find the best value if you had two items with weight limit W.”
Consider three items.
Take item 3.
Update leftover weight in the bag (W - W3).
Find the best value for the remaining two items and the new weight limit.
This is basically doing “Step 1” but with the new weight limit.
We reach a recursion.
Leave item 3.
The weight limit is the same (W).
Find the best value for the remaining two items with the weight limit W.
But wait! We have already solved this in “Step 1”.
We can directly use that value. This is Memoization.
Ultimately, the answer is the choice with the max value out of those two.
Let’s do this one more time, with N items.
Take item N.
Update leftover weight (W - Wn).
Find the best value for the remaining N-1 items with the new weight limit. We have two further choices:
Take N-1 item.
Update weight limit.
Find the best value for N-2 items…(recursion till N = 1 item).
Leave N-1 item.
The weight limit is the same.
Get the best value for N-2 items from already computed results.
Leave item N.
The weight limit is the same.
Get the best value for N-1 items from already computed results.
If we start from item 1 and keep computing at each item step, we can store those values, so that by the time we reach item N, when recursion happens, we use those stored values instead of computing them again.
That’s it. That’s the trick.
Start from the first item in any given array.
You have a choice— Take it or leave it. The intuition here— Any particular item in the array has only two possibilities. It is either part of the final answer, or it is not.
Compute results for each choice for each item and store them in a list or dictionary.
As you move from item 1 to item N and have to recursively do the same decision-making, use the stored values already computed.
With this understanding, see if you can write the code for this approach. Memoization and dynamic programming, the technical terms used to describe the above process, are some of the toughest concepts in algorithms. It definitely takes some time to wrap our heads around it. Practice makes it all much clear.
📨 Post Credits
Remember our little trivia about the first computer bug? It’s a testament to how even the simplest things can disrupt even the most advanced tech. As you go through your tech journey, always keep an eye out for those pesky "bugs" - whether they're in your code or flying around your workspace!
Fin.