Salesforce Interview Questions
- DSA
- LLD
- HLD
Q1: K-diff Pairs in an Array
Description: Given an integer array nums and an integer k, find the number of unique pairs (i, j) such that |nums[i] - nums[j]| == k and i != j. Pairs (a, b) and (b, a) are considered the same. Efficient implementation is usually via hashtable or two pointers for the sorted array.
Example 1:
Input: nums = [3,1,4,1,5], k = 2 Output: 2 Explanation: There are two 2-diff pairs in the array, (1, 3) and (3, 5). Although we have two 1s in the input, we should only return the number of unique pairs.
Example 2:
Input: nums = [1,2,3,4,5], k = 1 Output: 4 Explanation: There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
Example 3:
Input: nums = [1,3,1,5,4], k = 0 Output: 1 Explanation: There is one 0-diff pair in the array, (1, 1).
Q2: Maximum Number of Occurrences of a Substring
Description: Given a string, find the substring that appears the maximum number of times under two constraints: the substring’s length is between minSize and maxSize, and it contains at most maxLetters unique characters. Brute force with sliding window and hashmap for substring counting is typical.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4 Output: 2 Explanation: Substring "aab" has 2 occurrences in the original string. It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3 Output: 2 Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Q3: Number of Islands
Description: Given a 2D grid map of ‘1’s (land) and ‘0’s (water), count the number of distinct islands. An island is formed by connecting adjacent lands horizontally or vertically. Use DFS or BFS flood fill.
Example 1:
Input: grid = [ ["1","1","1","1","0"], ["1","1","0","1","0"], ["1","1","0","0","0"], ["0","0","0","0","0"] ] Output: 1
Example 2:
Input: grid = [ ["1","1","0","0","0"], ["1","1","0","0","0"], ["0","0","1","0","0"], ["0","0","0","1","1"] ] Output: 3
Q4: Longest Increasing Subsequence
Description: Given an integer array nums, return the length of the longest strictly increasing subsequence. Dynamic programming or patience sorting is commonly used (O(n^2) or O(n log n)).
Example 1:
Input: nums = [10,9,2,5,3,7,101,18] Output: 4 Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4.
Example 2:
Input: nums = [0,1,0,3,2,3] Output: 4
Example 3:
Input: nums = [7,7,7,7,7,7,7] Output: 1
Q5: LFU Cache
Description: Design and implement a data structure for Least Frequently Used (LFU) cache. It should support get and put in O(1) time on average. Balancing between least frequency and recent use requires double linked lists and hashmaps.
Example 1:
Input : ["LFUCache", "put", "put", "get", "put", "get", "get", "put", "get", "get", "get"]
[[2], [1, 1], [2, 2], [1], [3, 3], [2], [3], [4, 4], [1], [3], [4]]
Output : [null, null, null, 1, null, -1, 3, null, -1, 3, 4]
Explanation
// cnt(x) = the use counter for key x
// cache=[] will show the last used order for tiebreakers (leftmost element is most recent)
LFUCache lfu = new LFUCache(2);
lfu.put(1, 1); // cache=[1,_], cnt(1)=1
lfu.put(2, 2); // cache=[2,1], cnt(2)=1, cnt(1)=1
lfu.get(1); // return 1
// cache=[1,2], cnt(2)=1, cnt(1)=2
lfu.put(3, 3); // 2 is the LFU key because cnt(2)=1 is the smallest, invalidate 2.
// cache=[3,1], cnt(3)=1, cnt(1)=2
lfu.get(2); // return -1 (not found)
lfu.get(3); // return 3
// cache=[3,1], cnt(3)=2, cnt(1)=2
lfu.put(4, 4); // Both 1 and 3 have the same cnt, but 1 is LRU, invalidate 1.
// cache=[4,3], cnt(4)=1, cnt(3)=2
lfu.get(1); // return -1 (not found)
lfu.get(3); // return 3
// cache=[3,4], cnt(4)=1, cnt(3)=3
lfu.get(4); // return 4
// cache=[4,3], cnt(4)=2, cnt(3)=3
Q6: Minimum flips for fixed-length subarray
Description: Given a string s, return the total number of palindromic substrings in it. Overlapping substrings are counted as separate. Expand-around-center (O(n^2)) or DP approach.
Example 1:
Input: s = "abc" Output: 3 Explanation: Three palindromic strings: "a", "b", "c".
Example 2:
Input: s = "aaa" Output: 6 Explanation: Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa".
Q7: Balanced Brackets
Description: Check if a string of brackets (parentheses, curly braces, and square brackets) is balanced. Use a stack to validate opening and closing pairs.
Example 1:
Input: s = “()”
Output: true
Example 2:
Input: s = “()[]{}”
Output: true
Example 3:
Input: s = “(]”
Output: false
Example 4:
Input: s = “([])”
Output: true
Example 5:
Input: s = “([)]”
Output: false
Q8: Lowest Common Ancestor in Binary Tree
Description: Given a binary tree and two nodes, find their lowest common ancestor (LCA). Use recursion to traverse subtrees and return ancestor node.
Example 1:
Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1 Output: 3 Explanation: The LCA of nodes 5 and 1 is 3.
Example 2:
Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4 Output: 5 Explanation: The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
Q9: Stock Price Fluctuation
Description: Implement a structure that tracks stock price changes, and allows constant time update/retrieve of current, max, min prices. Typically uses hashmaps and heaps.
Example 1:
Input: ["StockPrice", "update", "update", "current", "maximum", "update", "maximum", "update", "minimum"]
[[], [1, 10], [2, 5], [], [], [1, 3], [], [4, 2], []]
Output: [null, null, null, 5, 10, null, 5, null, 2]
Explanation:
StockPrice stockPrice = new StockPrice();
stockPrice.update(1, 10); // Timestamps are [1] with corresponding prices [10].
stockPrice.update(2, 5); // Timestamps are [1,2] with corresponding prices [10,5].
stockPrice.current(); // return 5, the latest timestamp is 2 with the price being 5.
stockPrice.maximum(); // return 10, the maximum price is 10 at timestamp 1.
stockPrice.update(1, 3); // The previous timestamp 1 had the wrong price, so it is updated to 3.
// Timestamps are [1,2] with corresponding prices [3,5].
stockPrice.maximum(); // return 5, the maximum price is 5 after the correction.
stockPrice.update(4, 2); // Timestamps are [1,2,4] with corresponding prices [3,5,2].
stockPrice.minimum(); // return 2, the minimum price is 2 at timestamp 4.
Q10: Course Schedule
Description: Given prerequisites for courses, determine if it’s possible to finish all courses. Graph problem requiring detection of cycles (topological sort or white-grey-black DFS cycle detection).
Example 1:
Input: numCourses = 2, prerequisites = [[1,0]] Output: true Explanation: There are a total of 2 courses to take. To take course 1 you should have finished course 0. So it is possible.
Example 2:
Input: numCourses = 2, prerequisites = [[1,0],[0,1]] Output: false Explanation: There are a total of 2 courses to take. To take course 1 you should have finished course 0, and to take course 0 you should also have finished course 1. So it is impossible.
Q1. Car Rental System
Description: Design extensible classes for a car rental platform including user, vehicle, store, reservations, billing, payment and notification. Use OO patterns (Factory, Strategy, Observer, Singleton) to make system robust and easy to extend; describe persisting state and APIs for booking/cancel/search. No direct input/output.
Q2. Peer-to-Peer Parcel Delivery System
Description: Implement a P2P delivery platform covering onboarding, order lifecycle, auto-assignment of drivers, concurrency, notifications, and dashboard. Focus on modular, scalable, thread-safe code and separation of concerns. No direct input/output; detailed feature requirements only.
Q3. Spring Boot API Development (Pagination & Exception Handling)
Description: Develop layered REST APIs with service, controller, repository, and proper error handling/pagination. Secure endpoints and write unit/integration tests. No direct input/output, focus on software craftsmanship and organization.
Q4. Todo List App
Description: Implement CRUD for a to-do list, supporting add, delete, search, mark as complete, and query operations. Encapsulate logic into modular, reusable components.
Input: add(“buy milk”)
Q5. Student and Teacher Classes (Inheritance)
Description: OO design for persons at a school—shared and unique properties, with class inheritance for common methods and extended features (such as getTotalStrength(), getStudentInfo(), getTeacherSubjects()). No direct input/output; show extensible class diagrams and method signatures.
Q1. Twitter Hashtag System
Description: End-to-end design of large-scale system to manage hashtags for tweets: API endpoint design, sharding/indexing strategies for database, scalable read and write architecture, rate limiting, and load balancing. No input/output; focus on diagrams and scalability tradeoffs.
Q2. Tagging System Design
Description: System design for efficient, high-throughput tag storage on arbitrary items/content, requiring fast query and update, sharding/indexing for scale, with fine control over tag creation and deletion. No input/output; draw data model and scaling choices.
Q3. Notification Microservice Design
Description: Design a push notification microservice to reliably send notifications to users at scale, supporting retries, dead-letter queues, delivery tracking, horizontal scaling. No input/output; draw event flows, publish/subscribe patterns.
Q4. Scalability and DB Tradeoffs
Description: Discuss scenarios requiring horizontal and vertical scaling, compare RDBMS and NoSQL data models, and explore database optimization for real-world scale, reliability, and consistency challenges. No direct input/output, but summarise pros/cons in tabular/diagrammatic form.
Q5. Ride Sharing/Car Pooling System
Description: High-level design of a carpooling platform: ride matching, geographic microservices, state management, user and ride APIs, data modeling, scaling for massive user base; discuss CAP theorem, event queues, and failover. No input/output; provide APIs and service diagrams.