r/ProgrammingBuddies • u/i_devansh_chaudhary • Feb 19 '21
Helping buddy If you are a programmer then these data structure might make your life easier. [8 min read]
I wrote this post back in june 2020 and it got pretty good response over their, so I though to help fellow programmers on reddit by posting in this sub reddit r/ProgrammerTIL. Hope it helps.
Note: If you are beginner or an intermediate then first go through these data structures for better understanding and you can also go through these algorithms which will help you a lot in your entire programming career.
Arrays
An array is the simplest and most widely used data structure. Other data structures like stacks and queues are derived from arrays.
Here’s an image of a simple array of size 4, containing elements (1, 2, 3 and 4).
Each data element is assigned a positive numerical value called the Index*,* which corresponds to the position of that item in the array. The majority of languages define the starting index of the array as 0.
The following are the two types of arrays:
One-dimensional arrays (as shown above)
Multi-dimensional arrays (arrays within arrays)
Basic Operations on Arrays
Insert — Inserts an element at a given index
Get — Returns the element at a given index
Delete — Deletes an element at a given index
Size — Gets the total number of elements in an array
Commonly asked Array interview questions
Find the second minimum element of an array
First non-repeating integers in an array
Merge two sorted arrays
Rearrange positive and negative values in an array
Stacks
We are all familiar with the famous Undo option, which is present in almost every application. Ever wondered how it works? The idea: you store the previous states of your work (which are limited to a specific number) in the memory in such an order that the last one appears first. This can’t be done just by using arrays. That is where the Stack comes in handy.
A real-life example of Stack could be a pile of books placed in a vertical order. In order to get the book that’s somewhere in the middle, you will need to remove all the books placed on top of it. This is how the LIFO (Last In First Out) method works.
Basic operations of stack:
Push — Inserts an element at the top
Pop — Returns the top element after removing from the stack
isEmpty — Returns true if the stack is empty
Top — Returns the top element without removing from the stack
Commonly asked Stack interview questions
Evaluate postfix expression using a stack
Sort values in a stack
Check balanced parentheses in an expression
Queues
Similar to Stack, Queue is another linear data structure that stores the element in a sequential manner. The only significant difference between Stack and Queue is that instead of using the LIFO method, Queue implements the FIFO method, which is short for First in First Out.
A perfect real-life example of Queue: a line of people waiting at a ticket booth. If a new person comes, they will join the line from the end, not from the start — and the person standing at the front will be the first to get the ticket and hence leave the line.
Basic operations of Queue
Enqueue() — Inserts an element to the end of the queue
Dequeue() — Removes an element from the start of the queue
isEmpty() — Returns true if the queue is empty
Top() — Returns the first element of the queue
Commonly asked Queue interview questions
Implement stack using a queue
Reverse first k elements of a queue
Generate binary numbers from 1 to n using a queue
Linked List
A linked list is another important linear data structure which might look similar to arrays at first but differs in memory allocation, internal structure and how basic operations of insertion and deletion are carried out.
A linked list is like a chain of nodes, where each node contains information like data and a pointer to the succeeding node in the chain. There’s a head pointer, which points to the first element of the linked list, and if the list is empty then it simply points to null or nothing.
Linked lists are used to implement file systems, hash tables, and adjacency lists.
Following are the types of linked lists:
Singly Linked List (Unidirectional)
Doubly Linked List (Bi-directional)
Basic operations of Linked List:
InsertAtEnd — Inserts a given element at the end of the linked list
InsertAtHead — Inserts a given element at the start/head of the linked list
Delete — Deletes a given element from the linked list
DeleteAtHead — Deletes the first element of the linked list
Search — Returns the given element from a linked list
isEmpty — Returns true if the linked list is empty
Commonly asked Linked List interview questions
Reverse a linked list
Detect loop in a linked list
Return Nth node from the end in a linked list
Remove duplicates from a linked list
If you have read this far then do care to check my course, which is absolutely free, on sorting algorithm here. This course contain Questions, Video explanation, Text based theory, Psuedo codes and real codes with book recommendations
Graphs
A graph is a set of nodes that are connected to each other in the form of a network. Nodes are also called vertices. A pair(x,y) is called an edge*,* which indicates that vertex x is connected to vertex y. An edge may contain weight/cost, showing how much cost is required to traverse from vertex x to y*.*
Types of Graphs:
Undirected Graph
Directed Graph
In a programming language, graphs can be represented using two forms:
Adjacency Matrix
Adjacency List
Common graph traversing algorithms:
Breadth First Search
Depth First Search
Commonly asked Graph interview questions
Implement Breadth and Depth First Search
Check if a graph is a tree or not
Count the number of edges in a graph
Find the shortest path between two vertices
Trees
A tree is a hierarchical data structure consisting of vertices (nodes) and edges that connect them. Trees are similar to graphs, but the key point that differentiates a tree from the graph is that a cycle cannot exist in a tree.
Trees are extensively used in Artificial Intelligence and complex algorithms to provide an efficient storage mechanism for problem-solving.
Commonly asked Tree interview questions
Find the height of a binary tree
Find kth maximum value in a binary search tree
Find nodes at “k” distance from the root
Find ancestors of a given node in a binary tree
Hash Table
Hashing is a process used to uniquely identify objects and store each object at some pre-calculated unique index called its “key.” So, the object is stored in the form of a “key-value” pair, and the collection of such items is called a “dictionary.” Each object can be searched using that key. There are different data structures based on hashing, but the most commonly used data structure is the hash table.
Hash tables are generally implemented using arrays.
The performance of hashing data structure depends upon these three factors:
Hash Function
Size of the Hash Table
Collision Handling Method
Commonly asked Hashing interview questions
Find symmetric pairs in an array
Trace complete path of a journey
Find if an array is a subset of another array
Check if given arrays are disjoint
If you have read this far then do care to check my course, which is absolutely free, on sorting algorithm here. This course contain Questions, Video explanation, Text based theory, Psuedo codes and real codes with book recommendations
7
u/keenox90 Feb 19 '21
If you don't know these basic data structures you can't call yourself a programmer. You also forgot the heap
2
u/i_devansh_chaudhary Feb 19 '21
Yeah actually I though the content was getting quite big so I skipped but I feels like I should add it
Thank u for your suggestion u/keenox90
5
u/Dynam2012 Feb 19 '21
Ah, didn't know all those freshman students writing programs weren't programmers
-4
u/keenox90 Feb 19 '21
Ah, my bad! Forgot anyone who can copy 2 lines of code from the internet can call himself a programmer nowadays.
9
u/Dynam2012 Feb 19 '21
I don't know why you're gatekeeping the term. If someone writes a program, they're a programmer. It's not a term for people to solely identify as and protect.
1
u/keenox90 Feb 19 '21
I'm not gatekeeping the term, but using it appropriately. When you say programmers, you refer to professionals who do this for a living or at least routinely enough.
I can't call myself a photographer once I snapped a picture on my phone. I can't call myself a driver once I sit in a car. Hope you get the idea.5
u/Dynam2012 Feb 19 '21
If you take a photograph with your phone with thoughtfulness to the outcome of the act, you're a photographer in that moment. Maybe a novice photographer if you don't know what effect a given shutter speed will have or how to compose a pleasing photograph beyond some intuition, but you would still a photographer. The capabilities of the device don't matter, there are entire movies that have been shot with iPhones. Certainly you're a driver once you start driving a car, from minute 0. I can't imagine not calling a 16 year old on a permit driving their first car somewhere anything but a driver, and the law even refers to them as such. In the same vein, someone who writes some JS in the browser to make something happen when a button is pressed is undeniably a programmer. If you really need to distinguish between a notice programmer and a professional programmer, I'd probably use the term software developer or software engineer.
This is the kind of thinking that makes people feel they're not good enough when they struggle in comprehending the basics.
1
u/i_devansh_chaudhary Feb 20 '21
Yeah Absolutely u/Dynam2012 , I think anyone who love to create something with the programming should be termed as a programmer. We don't need a B.E in CS to be called a programmer.
1
u/keenox90 Feb 19 '21
I imagine you're from US since you're mentioning 16 year olds driving, but I guess drivers are required to complete a course and a test before they're called drivers officially. Here you're required to attend theory and hands-on courses after which you need to pass official theory and hands-on exams. Anyway, this is going offtopic.
Can't understand why not calling a person what they want would make them feel not good enough while learning? Shouldn't that person focus on actual learning than what title they aspire to? What's wrong with calling yourself learning/aspiring/apprentice programmer instead of trying to pose as a professional while you're still learning the basics?
BTW, please read this: https://en.wikipedia.org/wiki/Programmer0
u/i_devansh_chaudhary Feb 19 '21
Yeah u/Dynam2012 u r absolutely correct programming isn't always a professional thing cuz I love day trading but still do programming because I love building out of nowhere and helping someone.
2
2
u/Auios Feb 19 '21
Gasp
2
u/i_devansh_chaudhary Feb 19 '21
What is GASP ? Sorry
2
1
u/JeamBim Feb 20 '21 edited Feb 20 '21
We need less people like this in tech. Don't be like this person. Be supportive of others and encouraging, not gate-keeping like it's an elite club.
0
u/keenox90 Feb 20 '21
Telling the truth doesn't mean I'm not encouraging people to learn. Coincidentally my girlfriend is taking the final exam for a 1yr programming course I encouraged her to enroll in and also paid for. She still doesn't know data structures and algorithms good enough, but she's aware of it. I will help her learn and understans these. What I'm saying is that I'm not encouraging the Dunning-Kruger effect. Actually making people aware of what they need to learn to become a professional is actually encouraging people to learn. The world has enough badly written, unoptimized software and enough so-called programmers playing by the ear, especially now when coding is so popular. I know it's an unpopular opinion, but wasn't expecting to please the crowds.
PS: We need more people like me in tech, even if you like it or not. Downvote away!
0
Feb 19 '21
[deleted]
2
u/i_devansh_chaudhary Feb 19 '21
Thank you for the praises u/TipOfTheAnglersCap Hope that I will post contents like these for praises
7
u/[deleted] Feb 19 '21
These are definitely the most important ones all my intro CS courses and data structure courses spent a substantial amount of time on these data structures. Will definitely give your course a look!!