MongoDB Karat Interview Prep

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/45

encourage image

There's no tags or description

Looks like no tags are added yet.

46 Terms

1
New cards

One loop through a list of n elements

O(n) time.

2
New cards

Two independent nested loops, each over n elements

O(n²) time.

3
New cards

Python set add or membership check

Average O(1) time.

4
New cards

Storing up to n distinct elements in a set

O(n) extra space.

5
New cards

In place algorithm with only a few variables

O(1) extra space.

6
New cards

Why is binary search O(log n)?

Each comparison halves the remaining search range.

7
New cards

Iterative binary search extra space

O(1), using only a constant number of index variables.

8
New cards

Karat coding priority

Get a working solution first; optimize only if time permits.

9
New cards

Before saying a coding solution is complete

Spend 30 to 60 seconds checking edge cases.

10
New cards
BFS graph traversal time
O(V + E).
11
New cards
BFS graph traversal space
O(V), for the queue and visited set.
12
New cards
When to choose BFS
For a shortest path in an unweighted graph.
13
New cards
Can BFS and DFS both find connected components?
Yes. Either traversal can find connected components.
14
New cards
When should BFS mark a node visited?
When the node is enqueued, to prevent duplicate queue entries.
15
New cards
In standard BFS, is the queue ever larger than the visited set?
No. Every queued node is already visited, so queue size is at most visited size.
16
New cards
BFS queue space
O(V) in the worst case; it holds the current frontier.
17
New cards
DFS explicit stack space
O(V) in the worst case; it is O(height) on a tree.
18
New cards
Two sum in a sorted array
Use two pointers at opposite ends; move left when sum is too small and right when sum is too large. Time O(n), extra space O(1).
19
New cards
Anagram with a frequency map
Time O(n), extra space O(k), where k is the number of distinct characters.
20
New cards
When does a sum-equals-k sliding window work?
When all numbers are non-negative; then expanding can only increase the sum and shrinking can only decrease it.
21
New cards
Longest subarray with sum k when negatives are allowed
Use prefix sums plus a map from prefix sum to its earliest index. Time O(n), extra space O(n).
22
New cards
Height-balanced binary tree
Use postorder DFS: return each subtree height, or a failure sentinel if it is unbalanced. Each node is processed once: time O(n), recursion space O(h), worst O(n).
23
New cards
Longest substring without repeating characters
Use a sliding window and map character → latest index. On a duplicate inside the current window, move left to last_seen[char] + 1; never reset the whole window. Time O(n), space O(k).
24
New cards
Recursive vs iterative DFS
Use recursive DFS for concise tree or grid traversal when depth is safe; use an explicit stack when the traversal can be very deep or recursion limits are a concern. Both are O(V + E) time and O(V) worst-case space.
25
New cards
Merge sort complexity
There are log n split levels, and merging across each level touches all n elements: O(n log n) time. It needs O(n) extra space for merging.
26
New cards
27
New cards
First occurrence with binary search
On a match, store mid and keep searching left with right = mid - 1. Time O(log n), iterative extra space O(1).
28
New cards
29
New cards
3rd largest value in a stream
Keep a min-heap of size 3. Push each value; if size exceeds 3, pop the minimum. The root is the 3rd largest. Per item O(log 3) = O(1); space O(3) = O(1).
30
New cards
31
New cards
Valid parentheses invariant
Push expected closing brackets for opens. On a close, it must equal the top of the stack; otherwise return false. Finish valid only if the stack is empty. Time and worst-case space O(n).
32
New cards
33
New cards
When should I choose a stack?
Use a stack when the problem must resolve the most recently opened or unresolved item before earlier ones: a last-in, first-out relationship.
34
New cards
Stack examples
Matching brackets or tags, undo, DFS, and nested-expression evaluation.
35
New cards
36
New cards
Encapsulation — meaning and use
Bundle state and behavior in a class while controlling access to internal state. Use it to keep data valid and prevent unsafe outside changes.
37
New cards
Abstraction — meaning and use
Expose what an object can do while hiding how it does it. Use it when callers need a clear interface without implementation details.
38
New cards
Inheritance — meaning and use
A child class reuses and can extend a parent class’s data and behavior. Use it when related classes genuinely share functionality.
39
New cards
Polymorphism — meaning and use
Different object types respond to the same method call in their own way. Use it to work through one interface instead of type-specific conditionals.
40
New cards
Why can hash-map lookup be O(n) in the worst case?
The hash selects a bucket, then the map checks key equality. If n keys collide in one bucket, it may perform up to n O(1) equality checks: O(n) total.
41
New cards
Detect a cycle in a linked list with O(1) extra space
Use slow and fast pointers from head: slow moves 1 node and fast moves 2. If they meet, a cycle exists; if fast or fast.next is null, no cycle. Time O(n), space O(1).
42
New cards
Min-heap operation costs
Insert O(log n); retrieve minimum at heap[0] O(1); remove minimum O(log n).
43
New cards
Sorted array operation costs
Insertion O(n) worst case because elements shift; retrieve the endpoint minimum O(1); remove minimum is O(n) if it is at the front, or O(1) if sorted descending with the minimum at the end.
44
New cards
When should I use a min-heap versus a sorted array?
Use a min-heap when you repeatedly need the smallest item but do not need everything sorted, such as scheduling or retaining the k largest items with a size-k min-heap. Use a sorted array for ordered traversal, binary search, or range queries.
45
New cards
BFS vs DFS shortest path
BFS explores nodes in increasing number of edges from the start. It finishes distance 1 before distance 2, and so on. Therefore, the first time it discovers the target, that path uses the fewest edges. DFS may follow a long branch and find the target before exploring a shorter route.
46
New cards
Why mark visited when enqueuing in BFS?
It marks a node as already discovered, so other neighbors cannot enqueue duplicates. This avoids repeated work and keeps the queue bounded by the discovered vertices.