Data Structures & Algorithms Flashcards

1
Q

How are items in a Stack ordered?

A

LIFO, last in first out

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

What are common functions on a Stack?

A

Push, Peek, Pop

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

How does a Stack work?

A

Similar to an array in most languages, new items are added to the end of the list via push and removed from the end via pop

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

How does a Queue work?

A

New items are added to the end, and items are removed from the front

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

How are items in a Queue ordered?

A

FIFO, first in first out

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

How does insertion sort work?

A

Take number from unsorted pile and insert into sorted location

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

What is the runtime complexity of InsertionSort and why?

A

O(n^2), quadratic, because theres 2 nested loops. One loop for the input array, a second loop to determine where to insert each element into the output array from the input array

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

Identify the pattern(s) to use:

You are given an array prices where prices[i] is the price of a given stock on the ith day. You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock. Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.

Input: prices = [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.

A
  • brute force: compare every number against every other number, quadratic time complexity, o(n^2), because of nested loops
  • sliding window: create a dynamic window of days, o(n), linear bc only takes one pass
  • two pointers: both pointers start at 0, very similar to sliding window here, linear, o(n) bc it only takes 1 pass
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Identify the pattern(s) to use:

A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers. Given a string s, return true if it is a palindrome, or false otherwise.

Example 1:
Input: s = “A man, a plan, a canal: Panama”
Output: true
Explanation: “amanaplanacanalpanama” is a palindrome.

A
  • brute force: iterate the word and append to new string, then compare strings. runtime complexity: linear, o(n) bc we only iterate once. However, space complexity is also o(n) bc we made a new array
  • two pointers: place pointers at both ends of the word, iterate towards middle, comparing letters. Runtime is linear o(n) bc we iterate once, space complexity is constant, o(1) bc no new space is used
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Identify the pattern(s) to use:

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order.

Example 1:
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].

A
  • brute force: compare every number against every other number, quadratic, o(n^2) bc nested loops
  • hash: iterate and store in hash, identify diff. Runtime: o(n), linear, bc theres multiple iterations but not nested. spacetime is o(n) bc requires new space for hash.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly