Capital Group Technical Flashcards

(39 cards)

1
Q

What is a tree?

A

A hierarchical data structure made of nodes, with one root and child nodes.

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

What does each node have?

A

a value and references to children

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

What is a binary tree?

A

A tree where each node has at most two children: left and right.

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

What is a root?

A

top node

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

What is a leaf?

A

node with no children

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

What is depth/height?

A

distance from root

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

What is a subtree?

A

a node + its descendants

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

What is DFS?

A

depth first search - go as deep as possible before backing up

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

What is DFS used for?

A

searching, counting nodes, checking properties recursively

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

What is preorder?

A

node -> left -> right

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

What is inorder?

A

left -> node -> right

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

What is postorder?

A

left -> right -> node

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

What is the usual DFS skeleton?

A

def dfs(node):
if not node:
return

dfs(node.left)
dfs(node.right)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is BFS?

A

breadth first search - visit nodes level by level

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

What is BFS used for?

A

shortest path, level order traversal, nearest/minimum depth problems

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

What is the skeleton for BFS?

A

from collections import deque

queue = deque([root])

while queue:
node = queue.popleft()

 if node.left:
        queue.append(node.left)
 if node.right:
        queue.append(node.right)
17
Q

How do you explain the difference between DFS vs BFS?

A

DFS goes deep first using recursion or a stack, while BFS explores level by level using a queue

18
Q

What is a heap?

A

a special data structure where you can quickly get the min or max element

19
Q

What is a min heap?

A

smallest value on top

20
Q

What is a max heap?

A

largest value on top

21
Q

When are heaps used?

A

you need the top K elements, you need fast access to min/max repeatedly

22
Q

What is the syntax for heap in python?

A

import heapq

heap = []
heapq.heappush(heap, 5)
heapq.heappush(heap, 2)
heapq.heappush(heap, 8)

smallest = heapq.heappop(heap)

23
Q

What is the time complexity for heaps?

A

O(log n) insert and remove

24
Q

What are arrays usually used for?

A

scan once, track min/max, sliding window, two pointers

25
What is an example skeleton for arrays?
max_val = float('-inf') for num in nums: max_val = max(max_val, num)
26
What is the basic idea around DFS?
Process node, then recursively visit left and right children.
27
What is the basic idea of BFS?
Visit node, add children to a queue, process in order.
28
What is the maximum depth of binary tree leetcode problem?
def maxDepth(root): if not root: return 0 return 1 + max(maxDepth(root.left), maxDepth(root.right))
29
What is the invert binary tree leetcode problem?
def invertTree(root): if not root: return None root.left, root.right = root.right, root.left invertTree(root.left) invertTree(root.right) return root
30
What is the leetcode problem from binary tree level order traversal?
from collections import deque def levelOrder(root): if not root: return [] result = [] queue = deque([root]) while queue: level = [] for _ in range(len(queue)): node = queue.popleft() level.append(node.val) if node.left: queue.append(node.left) if node.right: queue.append(node.right) result.append(level) return result
31
What is the leetcode problem for same tree?
check if two binary trees are identical def isSameTree(p, q): if not p and not q: return True if not p or not q: return False if p.val != q.val: return False return isSameTree(p.left, q.left) and isSameTree(p.right, q.right)
32
What is the leetcode problem for validate binary search tree?
def isValidBST(root): def helper(node, low, high): if not node: return True if not (low < node.val < high): return False return helper(node.left, low, node.val) and helper(node.right, node.val, high) return helper(root, float('-inf'), float('inf'))
33
What is the leetcode problem for kth largest element in an array?
import heapq def findKthLargest(nums, k): heap = [] for num in nums: heapq.heappush(heap, num) if len(heap) > k: heapq.heappop(heap) return heap[0]
34
What is the leetcode problem for top k frequent elements
import heapq from collections import Counter def topKFrequent(nums, k): count = Counter(nums) heap = [] for num, freq in count.items(): heapq.heappush(heap, (freq, num)) if len(heap) > k: heapq.heappop(heap) return [num for freq, num in heap]
35
What is the leetcode problem for two sum?
def twoSum(nums, target): seen = {} for i, num in enumerate(nums): diff = target - num if diff in seen: return [seen[diff], i] seen[num] = i
36
What is the leetcode problem for maximum subarray?
def maxSubArray(nums): current = best = nums[0] for num in nums[1:]: current = max(num, current + num) best = max(best, current) return best
37
What is the leetcode problem for best time to buy and sell stock?
def maxProfit(prices): min_price = float('inf') max_profit = 0 for price in prices: min_price = min(min_price, price) max_profit = max(max_profit, price - min_price) return max_profit
38
What is the leetcode problem for contains duplicate?
def containsDuplicate(nums): return len(nums) != len(set(nums))
39
What is the leetcode problem for product of an array except self?
def productExceptSelf(nums): res = [1] * len(nums) prefix = 1 for i in range(len(nums)): res[i] = prefix prefix *= nums[i] suffix = 1 for i in range(len(nums) - 1, -1, -1): res[i] *= suffix suffix *= nums[i] return res