Computer Memory and Arrays
- 90Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
- spaceFlip card
- KI knew it
- DI didn't know it
- ←Previous card
- →Next card
Conceptually, how can computer memory be modeled?
As a large array of small boxes, each holding a fundamental unit of data.
Or swipe the card
90 cards
Conceptually, how can computer memory be modeled?
As a large array of small boxes, each holding a fundamental unit of data.
What is a memory address?
A sequential label identifying a particular location in the memory array.
What two basic operations can be performed on memory?
Writing data to an address and reading data from an address.
Why can memory be described as an array?
Because memory supports reading from and writing to a particular indexed location.
What is the foundational data-access problem underlying programming?
Finding the appropriate array index at which the desired data is stored.
How does a one-element memory array store a character?
It stores the character at index 0, the only available location.
How many possible orders are there for two distinct elements?
Two possible orders.
How many possible arrangements are there for three distinct elements?
Six, calculated as 3!.
What happens to the number of possible element orderings as array length increases?
It increases astronomically according to the factorial of the number of elements.
What is the main trade-off involved in ordering elements in an array?
Different orderings make different operations efficient while making others less efficient.
How would you determine whether an unsorted array of one million characters contains the letter Z?
Check every element from address 0 onward until finding Z or reaching the end.
Why is searching an unsorted million-character array for Z potentially slow?
Because Z may be absent or located near the end, requiring nearly every element to be read.
How can you quickly determine whether a sorted character array contains Z?
Read its final element; if it is Z, Z is present, and otherwise it is not.
Why does sorting characters alphabetically make the Z lookup trivial?
Because Z is the greatest character in the ordering and therefore must be the final element if present.
How would you find the most frequent character in an array?
Generally, search through the entire array to count or compare occurrences.
What ordering would make finding the most frequent character especially fast?
Order the characters by frequency, placing the most frequent character first.
What is the key relationship between an array's ordering and its performance?
The ordering determines which operations can exploit shortcuts and which require extensive searching or copying.
What is a data structure?
A strategy for finding and manipulating data stored in an array.
What are the two halves of a data structure?
The ordering of elements in the underlying array and the logic for operations on those elements.
What is a sorted array?
An array whose elements are arranged according to an ordering that can support efficient searches.
How can composite objects such as users be sorted?
They can be sorted according to a selected subfield, such as a user's ID.
How can the same user objects be made searchable by both name and ID?
Use separate arrays of indexes into the user array, with one sorted by name and another by ID.
What is the purpose of an index array in a multi-criteria data structure?
It stores references to objects in an order suited to a particular lookup criterion.
Why are pointers useful when organizing data by multiple criteria?
They identify where the actual data is stored without duplicating that data.
What library analogy illustrates the use of pointers or indexes?
A book remains on one shelf, while searches by title, author, or genre return directions to that shelf location.
What is an array list?
An ordered collection that supports indexed reads and writes while also allowing elements to be added and removed.
How does an array list add an element to its end?
It uses the current count as the next array index, writes the element there, and increases the count by one.
Why can an array list grow even though an array has fixed length?
Its backing array has extra unused capacity beyond the list's current number of elements.
What is the difference between an array list's capacity and size?
Capacity is the length of the backing array; size or count is the number of elements currently in the list.
What happens internally when an array-list element is removed from the middle?
Every subsequent element is copied backward by one position to fill the gap.
Why can a normal array not directly remove an individual element?
Its fixed indexes correspond to fixed memory locations, so removing one location would require removing physical memory.
What happens internally when an element is inserted into the middle of an array list?
Subsequent elements are copied forward to create space for the new element.
Why can insertion and removal be slow in a large array list?
They may require copying a large number of subsequent elements.
What general lesson do array-list operations illustrate?
A high-level language construct cannot eliminate the performance consequences of its underlying array representation.
What access restriction gives stacks and queues strong performance?
Elements are inserted or deleted only at designated ends rather than at arbitrary positions.
What is a stack?
A data structure in which additions and removals occur only at the last element, called the top.
Why is a stack called a stack?
It behaves like a vertical stack of blocks: blocks on top must be removed before reaching a lower block.
How is a stack implemented over an array?
With an array of fixed capacity and a variable tracking the number of elements.
How does pushing an element onto a stack work?
Write the element immediately after the current last element and increase the count by one.
What is popping from a stack?
Removing the element at the top of the stack.
Why is popping from a stack efficient?
The removed element is at the end, so no subsequent elements need to be copied.
What is a common use of a stack?
Tracking undoable changes in a text editor.
How can redo functionality be implemented with stacks?
Maintain a second stack containing redoable changes.
What is a queue?
A data structure that processes elements in the order they were added, allowing removal from the front and addition at the back.
Why are queues useful for processing web-server requests?
They respond to requests in arrival order instead of allowing newer requests to continually bypass older ones.
What is a dequeue operation?
Removing the item at the front of a queue.
How can a queue remove its front element without shifting all remaining elements?
Advance a start-index variable and treat the previous front location as no longer part of the queue.
What problem occurs if a queue's start index only increases?
Dead space accumulates at the beginning and the backing array eventually overflows.
How does a circular queue reuse space in a fixed-size array?
It accesses indexes modulo the array capacity so indexes wrap from the end back to zero.
What happens when a circular queue's end passes the backing array's final index?
The modulo operation wraps the next position around to index 0.
What is a linked list?
An ordered structure in which elements are kept in sequence by links to other elements rather than by their physical array order.
How can a linked list be implemented using an array?
Store each element in an array record together with the array index of the next element.
How is a new element linked into the middle of an array-based linked list?
Append it to the backing array and update the relevant next-index fields to point through it.
How is the final element represented in the linked-list example?
Its next-index field is set to −1.
What is the main insertion and deletion advantage of a linked list?
Elements can be inserted or removed without copying all subsequent elements, provided a neighboring element is directly referenced.
What is the main lookup disadvantage of a linked list?
It cannot efficiently access the element at an arbitrary position by index.
How would a naive linked list find the element at position 300,000?
Start at the first element and follow the next link 300,000 times.
Why can iterating through a linked list be slower than iterating through an array?
Linked-list elements may be accessed in a random memory pattern, causing poor cache performance.
Why are linked lists often used as building blocks despite their drawbacks?
Their flexible linking properties support more advanced structures, such as collision chains in hashmaps and relationships in trees.
At the lowest level, what are pointers or references conceptually?
Indexes into computer memory.
What is the heap in relation to dynamically allocated linked-list nodes?
An internal data structure provided by a language or library for managing dynamically allocated memory.
Why might linked-list nodes allocated with
newormallocperform worse than array-managed nodes?Generic heap allocation may introduce less predictable and less efficient memory access than managing nodes in an array with indexes.
What is a hashmap?
A data structure that uses a key, such as a string, to look up a stored value.
What is the key in a hashmap?
The data used to identify the value being looked up, such as a flower's name.
What does a hash function do?
It transforms an arbitrary key, such as a string, into a number.
How is a hash-function result converted into an array index?
Take the result modulo the length of the backing array.
What is a hashmap collision?
A situation in which two different keys produce the same array index.
How does single-array collision resolution work?
After a collision, search forward through the array until finding the matching key or an empty slot.
Why can the single-array hashmap approach form clusters?
Colliding items are stored in consecutive positions, creating long runs of occupied slots.
How does a two-array hashmap resolve collisions?
Store the first item for a hash in one array and store colliding items in a second array linked by next-item indexes.
What useful property of linked lists does the two-array hashmap exploit?
Multiple separate linked lists can be interleaved in one backing array because links, not physical adjacency, define each list.
Why would using an array list for every hashmap collision bucket be inconvenient?
It would require many separate allocations and make capacity planning difficult because collision distribution is unpredictable.
What is a tree data structure?
A structure of nodes connected by one-directional relationships from parent to child.
What are the elements of a tree called?
Nodes.
What are the two types of relationships in a tree?
A node above a relationship is the parent, and a node below it is the child.
How many parents can a tree node have, and how many children can a parent have?
Each child has only one parent, while each parent can have multiple children.
How can a tree be implemented using two arrays?
Store nodes in one array and store child relationships as linked-list records in a second array.
What information does a tree relationship record contain?
The index of a child node in the node array and the index of the next child relationship in the relationship array.
How is a particular child found in the two-array tree representation?
Follow the node's child index into the relationship array, then follow the relationship's node index back into the node array.
What is a graph?
A general relationship structure in which nodes can have multiple links from and to other nodes.
How does a graph differ from a tree?
A graph allows a node to have multiple parents or incoming relationships, whereas a tree allows only one parent per child.
How can linked lists, trees, and graphs be viewed as progressively related structures?
A linked list has one previous and one next link; a tree allows multiple next links; a graph allows multiple links in both directions.
What are graph connections called?
Edges.
What is A* used for?
Finding a shortest or best path between two points in a graph.
How are graphs commonly implemented with arrays?
Store nodes in one array and edges in a second array of linked edge records.
What is the main implementation difference between a tree and a graph?
Their underlying data arrangement can be the same; the difference is primarily in the relationships and operations permitted.
What additional storage may be needed to traverse a graph in both directions?
A duplicate of a vertex or relationship representation so the vertex is accessible from both connected nodes.
What practical strategy is recommended when approaching a novel programming problem?
Start with a simple array-based solution and see how far it can be extended.
Why should the choice of data structure depend on access patterns?
Every element ordering has strengths and weaknesses, so the best structure depends on the operations performed most often.
What is the central design principle illustrated by all the data structures in the content?
High-level data structures are different ways of organizing an underlying array and defining efficient operations over it.

Ask anything
We've read this whole study set, so questions about it get the best answers.