SP Data Structures and Algorithms 3 — Questions and Answers
Question 1: Which graph traversal algorithm uses a queue (BFS) and is best suited for finding the shortest path in an unweighted graph?
- Depth-First Search
- Breadth-First Search (Correct answer)
- Dijkstra's Algorithm
- A* Search
Correct answer: Breadth-First Search
BFS explores nodes level by level using a queue, guaranteeing the shortest path in unweighted graphs.
Question 2: A Salesforce architect must model Account hierarchy relationships for traversal. Which data structure best represents this?
- Stack
- Tree (Correct answer)
- Hash Table
- Circular Buffer
Correct answer: Tree
Account hierarchies are inherently tree structures with a root parent and child nodes at multiple levels.
Question 3: What is the worst-case time complexity of Quick Sort?
- O(n log n)
- O(n)
- O(n²) (Correct answer)
- O(log n)
Correct answer: O(n²)
Quick Sort degrades to O(n²) when the pivot is consistently the smallest or largest element, causing unbalanced partitions.
Question 4: In an Apex trigger that bulkifies record processing, why should SOQL queries be placed outside loops?
- To reduce heap size
- To avoid hitting the 150 SOQL query governor limit (Correct answer)
- To improve stack depth
- To prevent recursive triggers
Correct answer: To avoid hitting the 150 SOQL query governor limit
SOQL inside loops can quickly exceed the 150-query-per-transaction limit when processing large record sets.
Question 5: Which Apex Map method safely returns null instead of throwing an exception when a key is not found?
- get(key) (Correct answer)
- fetch(key)
- find(key)
- retrieve(key)
Correct answer: get(key)
Map.get(key) returns null if the key does not exist, avoiding a NullPointerException when handled properly.
Question 6: What is the space complexity of a hash table with n elements and no collisions?
- O(1)
- O(log n)
- O(n) (Correct answer)
- O(n²)
Correct answer: O(n)
A hash table requires O(n) space to store n key-value pairs.
Question 7: A developer sorts a List<String> of Salesforce record names alphabetically. Which built-in Apex method accomplishes this?
- List.order()
- List.sort() (Correct answer)
- Collections.sort()
- Array.sort()
Correct answer: List.sort()
The List.sort() method in Apex sorts elements in ascending order using natural ordering for comparable types.
Which graph traversal algorithm uses a queue (BFS) and is best suited for finding the shortest path in an unweighted graph?