Data Structures + Algorithms Flashcards

1
Q

Linked List

A

Data structure that stores individual elements (nodes) at different places in memory. Each node contains a value and the location of the next node

Implementation:

class node:

 def \_\_init\_\_(self, val, next=None):
      self.val = val
      self.next = next
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Double Linked List

A

Special type of linked list where each node contains the location of the next node AND location of previous node.

Allowed for reverse transversals.

Implementation:

class double_linked:

 def \_\_init\_\_ (self, val, next=None, prev=None):
      self.val = val
      self.next = next
      self.prev = prev
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Problems that involve being given numbers from range(1, n).

Generally involves arrays and require O(1) memory

A

Cyclic Sort

Implementation:

i = 0
nums = [2, 4, 1, 3]

while i < len(nums):

 j = nums[i]

 if nums[i] != nums[j]:
      nums[i], nums[j] = nums[j], nums[i]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Sliding Window

A

Requires two pointers, one used in for loop, and then another to keep track of front.

Iterate front pointer with while loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Tree Breadth First Search

A

Search algorithm that looks at all nodes at each current level before moving to next depth.

Time Complexity: Because iterating through each node once, O(n)

Space Complexity: O(n)

Implementation:

while queue is not empty:

 Remove node from queue and add     
 To current_level

 Check if left or right child exists 
 And add to queue
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Depth First Transversal Types

A

Inorder: Left, Root, Right
Preorder: Root, Left, Right
Postorder: Left, Right, Root

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Cyclic Sort

A

Hint: Given array of numbers from range(1,n)

Iterate over each item in list, if it does not equal the index then move number to correct index

Initialize:
i=0

while i

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Get each digit from number. Example:

num = 1657
Return 1, 6, 5, 7

A

num = 1657

while num % 10 >= 1:
digit = num % 10
print(digit)
num //= 10

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Valid Palindromes

A

Use two pointers

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Purpose: Fast and slow pointers

A

Used to detect cycles in iterable data

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Coding:

Create coding solution for linked list.

Transverse the list, printing out elements.

Reverse the list.

A

class LinkedListNode:

 def \_\_init\_\_(self, data, next=None):
      self.data=data
      self.next=next

class LinkedList:
def __init__(self):
self.head=None

 def add_node(node):
      if self.head:
           node.next=self.head
           self.head=node
      else:
           self.head=node
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Code:

Detect a cycle in a linked list

A

Fast and slow pointers

slow, fast = head, head

while fast and fast.next:
slow=slow.next
fast=fast.next.next

 if slow==fast:
      return True

 return False
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Recursive Functions

A

Base case: Every recursive function must have a base case. The base case is the simplest scenario that does not require further recursion. This is a termination condition that prevents the function from calling itself indefinitely. Without a proper base case, a recursive function can lead to infinite recursion.

Recursive case: In the recursive case, the function calls itself with the modified arguments. This is the essence of recursion – solving a larger problem by breaking it down into smaller instances of the same problem. The recursive case should move closer to the base case with each iteration.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Stacks

A

Stack: Linear data structure that operates with last in first out.

Push: Adds an element to the top of the stack.
Pop: Removes and returns the top element of the stack.
Peek (or Top): Returns the top element of the stack without removing it.
IsEmpty: Checks if the stack is empty.
Size: Returns the number of elements in the stack.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Queue

A

Linear data structure that has first in first out.

Enqueue: Adds an element to the end (rear) of the queue.
Dequeue: Removes and returns the front element of the queue.
Front (or Peek): Returns the front element of the queue without removing it.
IsEmpty: Checks if the queue is empty.
Size: Returns the number of elements in the queue.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Heap
(Min, Max)

A

A Binary Heap is a complete Binary Tree which is used to store data efficiently to get the max or min element based on its type. A Binary Heap is either Min Heap or Max Heap. In a Min Binary Heap, the key at the root must be minimum among all keys present in Binary Heap. The same property must be recursively true for all nodes in Binary Tree. Max Binary Heap is similar to Min Heap.