L2 18 and 19 Mar Sequences
L2 18 and 19 Mar Sequences
L2 18 and 19 Mar Sequences
Questions – Complexity?
Word RAM
Think 1 mins – (NO TALKING)
What is the memory address of the start of cell 6 of the array, assuming 0-
based indexing (i.e., cell 0 is the first cell of the array)?
Take a vote
1. 1004
2. 1023
3. 1024
4. None
Answer
1. 1004
2. 1023
3. 1024
4. None
Arrays as static sequences
STATIC ARRAYS
1. add elements
2. remove element
1. add elements
2. remove element
1. add elements
2. remove element
1. O(1), O(n)
2. O(1), O(1)
3. O(n ),
2 O(n)
4. O(n), O(n)
Solution
1. O(1), O(n)
2. O(1), O(1)
3. O(n ),
2 O(n)
4. O(n), O(n)
Arrays as dynamic
sequences
A sequence of elements to which we can
1. add elements anywhere
2. remove element from any anywhere
Think 2 mins – (NO TALKING)
You are given an Array List with 100 elements, and you want to remove the
element at index 7 (0-based indexing). How many "left-shift" operations will
you have to perform on the backing array?
Take a vote
1. 372
2. 92
3. 93
4. 368
Dynamic Sequence
ADT (Abstract Data Types)
Allen Newell (right) and Herbert Simon (left) (courtesy of Carnegie Mellon University)
Linked Lists
Linked Lists
Think 2 mins – (NO TALKING)
If I only have direct access to the head or tail pointer in a Linked List, and I
can only access the other elements by following the nodes' pointers, what is
worst-case time complexity of finding an element in a Linked
List with n elements?
Take a vote
1. O (n)
2. O (1)
3. O (n )
2
4. Do not know
Solution
1. O (n)
2. O (1)
3. O (n )
2
4. Do not know
Linked Lists
Think 3 mins – (No TALKING)
Write a function that starts at the
given node and returns true if the
element exists somewhere in
the Linked List, otherwise false if the
element does not exist in the Linked
List.
Array
Linked List
Summary Operation O( )
Static Dynamic Dynamic Dynamic
Array
1 n n n
Linked List
n 1 n n
Thank you.