0% found this document useful (0 votes)
22 views29 pages

Ace The Basics Final

Uploaded by

mrudulshah24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views29 pages

Ace The Basics Final

Uploaded by

mrudulshah24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

DECRYPTERS

Ace The Basics :


Data Structure Edition
OVERVIEW
Introduction about data structures
Stack
Linked List
Queue
Vector & iterator
Binary Search
Quiz
Q&A
DATA STRUCTURES
A data structure is a way of organizing, managing,
and storing data to enable efficient access and
modification.
Examples include arrays, linked lists, stacks, queues,
trees, and graphs. Choosing the right data structure
helps improve the efficiency and performance of
algorithms.
An algorithm is a step-by-step procedure or set of
rules designed to perform a specific task or solve a
problem efficiently.
STACK
Defination
it is a linear list that allows insertion and deletion at only one end.
It is a Data Structure that follows LIFO Order. i.e., Last in First Out.

Operations on stack
push- To add an element at the top.
pop- To remove an element form the top.
Top- Top most element on the stack.
isEmpty- To checkif Stack is empty or not.
REAL WORLD EXAMPLE
CONSIDER A BOX OF PRINGLES
(FAMOUS CHIPS BRAND) YOU
WILL TAKE OF CAP AND WILL
TAKE ALL CHIPS ONE BY ONE
FROM THE TOP, AND IF YOU
CANNOT EAT, YOU WILL PUT IT
BACK ON TOP.
TECHNICAL EXAMPLE
You all have used Undo and Redo in
Computer it is nothing but implementation
of stack.

Compiler also uses Stack to check if


Parenthesis is balance or not will also see
its implementation by writing code
.
Expression Evaluation and Parsing.

How all can we forget the monster of


Programming Recursion Concept
WE CAN IMPLEMENT THIS BY EITHER
LINKED LIST OR ARRAY
Both ways of Implementation
Result in the same T.C
push: - O(1)
pop: - O(1)
Top: - O(1)
isEmpty: - O(1)
Questions
Valid Parenthesis

https://leetcode.com/problems/valid-parentheses/description/

Reverse Words in a String

https://leetcode.com/problems/reverse-words-in-a-string/

Remove All Adjacent Duplicates In String

https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/description/?envType=problem-list-
v2&envId=stack&status=SOLVED
LINKED LIST
A Linked list is a linear data structure where elements(nodes) are stored at
non-contiguous memory locations. Each node contains data and a reference(or
pointer) to the next node.

NODE STRUCTURE
Data: The value or data the node
holds.
Pointer/Reference: Points to the next node in the
sequence.
TYPES OF LINKED LISTS:
Singly Linked List: Each node points to the next node in
the sequence. The last node points to null or None.
Doubly Linked List: Each node points to both the next
and the previous nodes, allowing traversal in both
directions.
Circular Linked List: The last node points back to the
first node, forming a circle.
BASIC OPERATIONS
Insertion: Adding a new node to the list
(beginning, end, or any position).
Deletion: Removing a node from the list
(beginning, end, or any position).
Traversal: Visiting all nodes in the list to access
or manipulate data.
Searching: Finding a node with a specific value.

1 2 3
Advantages Disadvantages

01 Dynamic Size: Unlike arrays, 01 Memory Usage: Requires


linked lists can grow or extra memory for storing
shrink as needed. pointers.
02 Efficient Insertions/Deletions: 02 Sequential Access: Nodes must
Adding or removing nodes can be accessed in order, making
be done without shifting it slower for accessing a
elements like in arrays specific node compared to
arrays.
Questions
Print List in Reverse
https://www.hackerrank.com/challenges/print-the-elements-of-a-linked-list-in-reverse/problem

Linked List Cycle


https://leetcode.com/problems/linked-list-cycle/description/

Merge Nodes in between zeros

https://leetcode.com/problems/merge-nodes-in-between-zeros/description/?envType=problem-list-
v2&envId=linked-list
Intersection of two Linked List
https://leetcode.com/problems/intersection-of-two-linked-lists/description/?envType=problem-list-
v2&envId=linked-list
QUEUE
Defination
A queue is a type of data structure in which elements are
added at one end, called the rear, and removed from the other
end, called the front.

Works on the principle of First In, First Out


(FIFO)
TECHNICAL EXAMPLE
In mobile games like BGMI, Free Fire,
Call of duty, we can see
implementation of queue while
matchmaking.

When we listen to songs in Spotify or


You tube music or watch videos in You
tube, we get an option of Add to
queue. So, first added song/video plays
first.
OPERATIONS ON QUEUE:
push(b) :- Adds the element ‘b’ at the end of the
queue.
pop() :- Deletes the first element of queue.
empty() :- Returns true if queue is empty.
size() :- Returns size of queue.
front() :- Returns first element of queue.
back() :- Returns last element of queue.
TYPES OF QUEUE:
1. Circular queue :- In this type of queue the last position
is connected back to the first position to make a circle.
2. Double ended queue :- It is also a Queue data structure
in which the insertion and deletion operations are
performed at both the ends (front and rear).
3. Priority Queue :- It is a special type of queue in which
each element is associated with a priority and is served
according to its priority (ascending or descending).
Insert Front Front Rear Insert Rear

Element Element Element

Delete Front Delete Rear


VECTORS:
Defination
A vector is a dynamic array that can grow or shrink
in size, allowing elements to be added or removed
efficiently.
Uses of Vectors:
1. Dynamic Resizing
2. Efficient Insertion/Deletion
3. Random Access
4. Temporary Data storage
5. Multidimensional Array
COMMON OPERATIONS ON VECTORS:

1.push_back(value)
2.pop_back()
3.size()
4.capacity()
5.resize(size)
6.at(index)
7.clear()
8.empty()
9.insert()
1 2 3 4 5 10.erase()
ITERATORS
Iterators are abstract pointers used to access and
traverse the elements of a data structure, such as
vectors, maps, sets, etc. in a consistent and controlled
manner.
Example
Declaration of vector iterator: - vector<int>::iterator;
Declaration of map iterator: - map<int,int>::iterator;
vec.begin() {Points to first element} and vec.end() {Points to
non-existent element which is next to last} are some of the
iterators known to us.
Iterate through the container:
for (auto it = v.begin() ; it != v.end() ; it++)
Questions
Numbers of recent calls

https://leetcode.com/problems/number-of-recent-calls/description/
front middle back queue

https://leetcode.com/problems/design-front-middle-back-queue/description/?envType=problem-
list-v2&envId=queue&difficulty=MEDIUM
Number of students unable to eat lunch
https://leetcode.com/problems/number-of-students-unable-to-eat-lunch/description/?
envType=problem-list-v2&envId=queue&difficulty=EASY
BINARY SEARCH
Binary search is a search algorithm used
to find the position on of a target value
within a sorted array.

It works by repeatedly dividing the search


interval in half until the target value is
found or the interval is empty.

The search interval is halved by


comparing the target element with the
middle value of the search space.
VISUAL REPRESENTATION
Advantages Disadvantages

01 Efficient: Binary search has a


01 Requires a sorted array:
me complexity of O(log n), which
Binary search only works
makes it very efficient for
searching large sorted arrays.
on sorted arrays. If the
array is not sorted, it
02 Reliable: Binary search is a must be sorted before
reliable algorithm that will binary search can be
always find the target used.
element if it exists in the
array.
Questions
Implementation
https://leetcode.com/problems/binary-search/description/

Square root of Number


Number
https://www.geeksforgeeks.org/problems/square-root/0?
utm_source=youtube&utm_medium=collab_striver_ytdescription&utm_campaign=square-root
Find the Smallest Divisor Given a Threshold

https://leetcode.com/problems/find-the-smallest-divisor-given-a-threshold/description/
THANK
YOU

You might also like