How does the data structure contribute optimally ?!

Tram Ho

Many companies (most of them specialize in technology) require candidates to know algorithms and data structures. It sounds strange, I don’t know if it’s okay, it’s just setting up a few objects, arrays, and maps.
The reason is that these companies have very high software quality standards, their engineers need to be able to program, not just develop on existing ones. On the other hand it shows the candidate’s programming and software development mindset, the ability to go far and deep into their technologies in the future.
Within the scope of this article, I will mention some practical case studies for you to easily visualize: (but there are still many and very complicated)

OPTIMIZE 2-dimensional array with 1-dimensional array

Note: if each element has different array size, it is not possible.
If you ever want to use a 2-dimensional array (also called an array containing an array) then you can think of a 1-dimensional array is enough.
For example, we need an array of 10 rows of 10 columns. i and j are indexes in column and row respectively. From there, the array access will be of the form arr [i] [j].
Well, what does it matter ?! Good run !! … The problem is that when we allocate memory 10 arrays, each one is located somewhere, without continuity, which will affect the access speed.
To be more optimal: we only need an array with 10 * 10 = 100 elements. Then the position at (i, j) will be the only index which is calculated as follows:

100 contiguous elements, or 100 contiguous pointers of course is much more ideal. Huh ?!
The most common application is that image data never has [] [] bytes but only [] bytes. Or if you make checkers game, chess / king, the chess board should be a one-dimensional array.

OPTIMAL STORAGE FOR COLOR OR IP

Okie this backend problem is more common. Now suppose we have 10 million lines of data, including color information (red, green, blue) or IPv4 (abcd format), why?
There are 2 common ways to save: save 3 columns R, G and B or 1 column as string containing hex format of color (“ffeedd”).
From there: if we store 3 columns of type integers we lose at least 4 × 3 = 12bytes. If it is string then 1 * 6 = 6bytes.
Actually we only need 1 column of integers (4bytes) is enough. The problem is how to convert it.
Talk about color. We have 3 channels, each with a size of 8bit = 2 ^ 8 = 256 values ​​(0-255). Each color has a total size of 24bit. You can understand that 24 digits 0 and 1 are adjacent. Calculating the actual bytes is only 3.
The simple way to change is using bitwise operations:

How to do the same with IPv4 / v6
Well … What if I need to change it backwards ^^.

This option on the one hand has greatly reduced storage capacity, on the one hand if we index the integer column it will always be much more efficient than typing for 3 columns or string columns.

LINKED LIST FOR THINGS NOT REQUIRED THROUGH INDEX OR HIGH REMOVE / ADD RATE

I think it should not talk about the array when append and remove the item, what it is in the article because it will be quite long. But basically, people can understand that the array is a sequence of continuous memory, accessing the index very quickly. When we remove / add an element from an array, it depends on the language, but most will initialize a new array.
The linked list is also a list, but the element has a link to another element like a train. This feature helps linked-list remove / add very quickly, because only need to change the link. On the other hand, accessing by index is very slow because you have to travel each element.
Okie in school is like that but there is no case where to use !! Actually yes, just whether we want it or not.
Think about the carousel component we often use. I used linked-list for it and it feels much better. Why: because most of the time we just need to next (), back () or goTo (step) and paginate (load more to append), sometimes we need to remove any one item. Also if we need the list to be a circle to loop, …
Yes, this is the linked-list stage.
In addition, we also have use cases: breadcumb, category, folder (tree), … Well and the DOM tree (DOM Tree) is also a realization of that linked-list. The mechanism for remembering permissions for processes in linux is also linked-list. Oh, stop talking.

SUMMARY

If you do not use the things I mentioned above, it’s okay, just solve the problem properly and enough is okie. If you want to go further, or apply and big tech companies, you can spend more time with them. After all, they are also interesting and somewhat beautiful (beautiful) there
Share the news now

Source : Techtalk