DSA - Arrays, Linked Lists & ADTs Flashcards

1
Q

Define an Array

A

Data Structure stored as contiguous list of cells OR sequences of bytes in memory.

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

What does INDEXING mean?

A

Giving each element in the array a value, first cell with have index 0 and final cell will have index ((length of array) -1 ).

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

What is a TYPE?

A

Type refers to the DATA TYPE of the Data Structure.

OR

A set of Possible values (ADTs)

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

What is the time access any element in the array?

A

Constant Time regardless of the index

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

Code to create ARRAY

A

int[ ] nums = new int [4]

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

IN int[ ] nums = new int [4] what is nums?

A

The array name OR identifier

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

IN int[ ] nums = new int [4] what does int[] do?

A

Declares the datatype of Arrays

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

IN int[ ] nums = new int [4] what does new do?

A

Allocates memory for the Array

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

IN int[ ] nums = new int [4] what does 4 represent?

A

Declares how many elements are in our Array

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

How to get values from Array?

A

DT val = nums[0],
where DT = Datatype

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

In Datatype val = nums[0] what does DT val represent?

A

Initialises the variable value and assigns the datatype

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

In Datatype val = nums[0] what does num[0]?

A

nums[0] declares the array and which index value we want

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

How to Assign Values in Arrays?

A

nums[i] = 23 , where i is an index in the Array

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

How to find Length of the Array?

A

len = length(nums)

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

Why is it important to use length of the Array?

A

Important when using if statements OR loops to find values in the Arrays or add items to an array.

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

How much memory does an integer occupy?

A

4 bytes

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

How do you calculate OFFSETS?

A

Index of Element * NO. bytes for Data type

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

List ADTs using ARRAYS

A

List ADTs and arrays are more sophisticated than Arrays, however tend to use basic Arrays when implementing them;

ADTs lists have more complex operations i.e- list size, sort list, concatenating list, etc.

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

Memory Management using Java:

A
  • Memory allocation is automatic
  • Freeing memory is automatic (Using garbage collector)
  • Bounds of arrays are checked (Check if array is out of range)
  • Slow & Safe
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Memory Management using C/C++

&

What does Explicit mean

A

1- Allocation are explicit
2- Freeing memory is explicit
3- Bounds are not checked

  • Fast & Dangerous

Explicit 1) Need to allocate space & include Data Type
2) Must free up allocated space otherwise causes error and Data will never get removed.

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

Common Mistakes in Array

A

int[ ] nums = new int [4]
int[4] is an error
The list has no index 4, the range of the index is 0-3

Java will return an error but in C/C++ will lead to corruption of data in memory

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

Define What a Linked List is:

A
  • Data structure in which the objects are ordered linearly
  • Order determined by the pointer in each object (NODE)
  • Collection of NODES connected to each other
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What is a Singly Linked List ?

A

Each element has a next pointer NO prev pointer
(CAN ONLY POINT TO NEXT NODE)

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

What is Doubly Linked List?

A

Can point to prev node as well as next node
(CAN BE CIRCULAR OR NOT DEPENDS IF FIRST NODE POINTS TO LAST)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
What is a Sorted Linked List?
Linear order of list corresponds to linear order of key stored in elements of list
26
What is Not Sorted Linked List?
Elements appear in Any order
27
What is a Circular Linked List?
All nodes can point to prev & next node, First points to last node for prev node and last points to First node for next pointer
28
What is a Non Circular Linked List?
Can Be anything else but a circular Linked List
29
What is a pointer?
Points to the next node, Stores the address of the following block
30
What is the END pointer?
- Indicates end of list - NULL can be used to represent END (Can also be -1/0) - It is an invalid reference therefore cannot be used to continue list
31
What does List Variable do?
Contains a head pointer that holds the address pointing to first node.
32
Does Linked List need to be Stored in Order in Memory?
NO
33
Is Linked List Fixed?
No, so you can insert and delete items you want
34
Difference in Array and Linked List to find specific position ?
Array uses index to get to a position. Linked List requires you to traverse through entire list to get to that position.
35
How to Search through Linked List?
To Find an element: - Traverse it by checking value in each cell and continue checking by using pointer to check next nodes. - Continue traversal till you find the item or reach end pointer
36
WORST Time Complexity of Linked List:
O(n) --> due to searching entire list & list length is = n
37
Define Constant
No. of Operations does not depend on No. of elements
38
Define Linear
Increase the number of elements by factor n, then operations cost will also increase by a factor n (WORST CASE)
39
What does the command/code a.val do?
Outputs the value of the Node it is on
40
What does the command/ code a.next do?
Points to the next node from the node a
41
What does the command/code a.prev do?
Points to the prevoius node from node a - Doubly Linked List ONLY
42
What does the command/ code a.next.val do?
Outputs the value on the next node from node its on
43
Define ADTs :
Man Made/ Programmer developed Data Type implemented for a specific reason -LIST is an ADTs
44
How do you specify a List in JAVA
List --> list of INTS List --> List of Strings List = new ArrayList(); --> creates a list with a variable length List = new LinkedList(); --> creates a list with a variable length
45
How to Write an Array based List?
ArrayList
46
How to Write an Linked List based List?
LinkedList
47
What are ADTs
Types with associated operation, whose representations is hidden to the user. All the other operations of ADT user needs to be specified
48
Is the List type int if a list of integers contain the type int?
No it is a CONTAINER TYPE which is more complex
49
What is Recursive Algorithm?
- A function/method that calls itself -less code --> less Internal memory - More RAM space required - Runs till meets or Completes BaseCase -Faster / Easier to code
50
CODE for Inserting into a ARRAY by SHIFTING UP:
maxsize = 100 Point [] locations = new Point [maxsize]; int size = 0; VOID insert (int pos, POINT pt){ if (size == maxsize ){ throw new ARRAYFULLEXCEPTION("Location_array") } for (int i = size -1; i >= pos ; i-- ){ // copy entry in pos i one pos towards the end locations[i+1] = locations[i]; } locations[pos] = pt; size++; }
51
What is a Durable Array?
It: -Provides Maxsize -Gives illusion that you are changing size -Make sure you don't go over Maxsize
52
CODE to DELETE Beginning of Linked List
if list = END then ERROR("Cannot Remove a node from empty-list") else list= list.next;
53
CODE to DELETE End of Linked List
if list = END then ERROR("Cannot Remove a node from empty-list") else if list.next == END then list = END; else while cursor.next.next != END do cursor = cursor.next; end cursor.next = END;
54
How to Create a Node in Linked List?
Class Node{ int val; Node next; } Node list = END;
55
CODE to insert at Beginning of the List:
void insert_beg(Node list, int number){ newNode = new Node(); newNode.val = number; newNode.next = list; list = newNode; }
56
CODE FOR LOOKUP :
int value_at (Node list, int index){ int i = 0; Node nextnode = list; while (true){ if (nextnode == END){ throw new OutOfBoundException(); } if (i== index){ break; } nextnode = nextnode.next; i++; } return nextnode.val; }
57
What is CODE for is_empty() | LL
Boolean is_empty(NODE list) { return (list == END); }
58
CODE Insert at END of Linked List
void insert_end(NODE list, int number) { newblock = new Node(); newblock.val = number; newblock.next = END; if (list == END) { list == newblock; } else { cursor = list; while (cursor.next != END) { cursor = cursor.next; } cursor.next = newblock; } }
59
CODE Insert at Beginning of Linked List
void insert_beg (NODE list, int number ){ newNode = new Node(); newNode.val = number; newNode.next = list; list = newNode;
60
What does the First Block in a Linked List hold?
Stores a number/ the data
61
What does the second Block in a Linked List hold?
Stores the address of the following block
62
What does List operation Last Element do?
- First Check if entire list is empty, if it is return Error - If not Empty Check if rest of list except first element is empty return first element, -Otherwise Recursively go through the rest of the list bare first element to find the last non-empty value
63
What is the CODE for List operation Last Element?
last(lst) { if( isEmpty(lst)) error("Error: _empty_list_in_last") else if (isEmpty(rest(lst))) return first(lst) else return last(rest(lst)) }
64
What does the Constructor EmptyList do?
Returns an empty List
65
What does the Constructor MakeList(element, list) do?
Adds an element at the front of a list
66
What does the Accessor first(list) do?
Returns the first element of the list
67
What does the Accessor rest(list) do?
Returns the all elements of list bare First
68
What does the Accessor isEmpty(list) do?
Reports whether a list is empty
69
What is the CODE for List operation getElementByIndex?
getElementByIndex(index, lst) { if( index <0 or isEmpty(lst)) error("Error:_index_out_of_range") elseif (index == 0) return first(lst) else return getElementByIndex (index -1, rest(lst)) }
70
What is the CODE for List operation append?
append (lst1,lst2){ if(isEmpty(lst1)) return lst2 else return MakeList(first(list1), append(rest(lst1), lst2) }
71
What does the List operation append?
Joins or combines 2 lists together - Adds first element on from lst1, then rest of lst1 till it is empty and then lst2 till it is empty - which creates a new list