Hello Devs! Welcome to the Instant Tea section from CodersTea.in. Here I will directly jump to the solution i.e. “Show me the Code, Damn it”. If you need to know the code in more detail, then let me know in the comments. I will add the link to my (old or the one I will be writing) or other resources for you.
Let us quickly jump to the solution for the problem you asked 🤔
Easily Calculate Memory From Bytes to KB, MB, GB, TB
Here is the list with numbers of bytes for each size. With this, you can quickly identify which size metric to show instead of saying a number of bytes. Very helpful for your next System Design Interview.
1 Byte = 8 bit. For simplicity, I am using 1000 as the base instead of 1024 for KB and further calculations. You can mention this to the interviewer to make them aware.
|Size||Abbr.||Number of |
|Number of Bytes|
|KiloBytes||KB||Thousands – K||3||1,000|
|MegaBytes||MB||Millions – M||6||1,000,000|
|GigaBytes||GB||Billions – B||9||1,000,000,000|
|TeraBytes||TB||Trillions – T||12||1,000,000,000,000|
|PetaBytes||PB||Quadrillion – Q||15||1,000,000,000,000,000|
|ExaByte||EB||Quintillion – 🤷♂️||18||1,000,000,000,000,000,000|
|ZettaByte||ZB||Sextillion – 🤷♂️||21||1,000,000,000,000,000,000,000|
|YottaByte||YB||Septillion – 🤷♂️||24||1,000,000,000,000,000,000,000,000|
How to Use This to Calculate Size in System Design Interview
Example with Database Record
Let’s say I am in a system design interview. I want to store N numbers of records. N can be any number from thousands to Quadrillion. Each record is approximately 1000 Bytes.
Here is how I can solve this for various values of N. Always remember that in System Design Interview assumption is in your hand (most of the time), so choose a number that would be easier to calculate. like 100 Million, 50 Million, 1 Billion etc. Don’t choose any arbitrary number like 333 million, or 43.8 thousand.
N = 100 Million. Records are 1000 Bytes each. I will do, 100 Million (from records) * 1000 (record size) = 100 * 1 Million * 1 Thousand. The multiplication result would be the addition of zeroes 😉. That would be 2 zeroes + 6 zeroes + 3 zeroes (from 100, 1M, and 1K respectively) = 11 zeroes = 100 Billion. From the list, it would become 100 GB.
For N = 40 Million. Number of zeroes would be 40 * 1 M * 1 K = 1 Zero + 6 Zeroes + 3 Zeroes = 10 Zeroes = 10 Billion. Taking 4 of 40 would be 4 * 10 Billion = 40 Billion = 40 GB.
Example with File
Another example is where I have M number of audio files with an average size of 7MB. Again, M can be any number, 80 Million, 1 Billion, etc.
M = 70 Million. 7 MB * 70 Million. 6 Zeroes from 7 MB and 7 Zeroes from 70 MB. It would become 6 + 7 = 13 Zeroes = 10 Trillion = 10TB. And both are 7 so 7 * 7 = 49 so it would be 10 * 49 = 490 TB. or 0.49 PT, rounding off to 0.5 PT
You can quickly go through the list before your system design interview for reference.
It’s Done, But one more thing You can Do
And yes, that’s your solution there. I hope this will work for you. Let me know in the comments if you have any other solution. See all other Instant Tea posts here. Before going, do connect with us on social media. It’s @coderstea on Twitter, Linkedin, Facebook, Instagram, and YouTube.
Also, please subscribe to our newsletter to know about the latest updates from CodersTea.
See you in the next post.