Ch 1 : Arrays & Strings Flashcards

1
Q

Overview:

Some important

Data Structures

and

Techniques

for manipulating

Arrays and Strings

A
  • Hash Tables
  • ArrayList and Resizeable Arrays
  • String Builder
  • Concatenation
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a

Hash Table?

A

A data structure that maps keys to values for highly efficient lookup.

There are a number of ways of implementing this.

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

Hash Table:

Common Implementation

A

Components:

  • Array of Linked Lists
  • Hash Function
  • Key Value Pairs stored in the Linked Lists

Insertion:

  • Given a Key, Hash Function produces Hash Code which is an index in the array
  • The Value to be stored is placed in the Linked List at that index

Retrieval:

  • Given a Key, Calculate Hash Code from Key
  • Search for Key in Linked List and return associated Value
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Hash Tables:

The most basic common Hash Function

A

Hash Code

=

Key % array_length

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

Hash Tables:
Two Simple Implementation Approaches and their Access Time

A
  1. Array of Linked Lists with Hash Function,
    Access Time: O(n)
  2. Balanced Binary Search Tree
    Access Time O(log n)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Hash Table:

Benefits of Balanced Binary Search Tree

Implementation

A
  • Lookup time is O(log N)
  • Uses less space compared to allocating a large array
  • Can iterate through the keys in order, which can be useful sometimes
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Hash Table:

Complexity Analysis

A

If number of collisions is high,

Worst Case Runtime is O(N), where N is the number of Keys

If Keys are distributed well by the Hash Function and size of array,

Lookup Runtime is O(1)

If implementation uses a balanced Binary Search Tree, less space is used for the array and Lookup Runtime is O(log N)

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

Arrays vs Lists

A

In some languages, arrays are automatically resized to accomodate new elements.

These are often called “Lists” rather than “Arrays”, or “Resizeable Arrays”

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

What is an

ArrayList

A

An array-like data structure

that offers dynamic resizing.

It resizes itself as needed,

while maintaining O(1) access time.

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

ArrayList

Common Implementation

and

Complexity

A

Maintains an normal array of elements

When the array is full, copies elements into an array twice the size.

Access/Insertion time is O(1) just like a normal array.

Doubling takes O(N), but happens so rarely that this time can be Amortized so insertion time is still O(1) overall.

Different Implementations may use a different “Sizing Factor” than 2.

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

What is a

StringBuilder

A

A Data Structure that efficiently concatenates multiple strings

by placing them all in a resizeable array until the final string is needed.

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

String Concatenation:

Normal Concatenation

vs

String Builder

A

Normal Concatenation:

  • A new String object is created
  • Both of the old String Objects are copied over
  • A new string is created for every copy operation
  • If all strings are of length X:
    • O( x + 2x + . . . + nx) = O(xn2). = O(n2)

String Builder

  • Adds strings to a resizeable array of strings
  • when toString() called, creates one new string and copies all the strings it has stored to it.
    • O(xn) = O(n)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Arrays and Strings:

Common Types of Questions (9)

A
  • Unique
  • Check Permutation
  • Palindrome Permutation
  • Insert/Replace
  • Number of Edits(“One Away”)
  • String Compression
  • Matrix Rotation
  • Zero Matrix
  • String Rotation
How well did you know this?
1
Not at all
2
3
4
5
Perfectly