Skip to main content

Data Structures & Algorithms Interview Questions

Review this list of 264 Data Structures & Algorithms interview questions and answers verified by hiring managers and candidates.
  • +2

    "#include #include bool palindrome(std::string &str, int left, int right, int error) { if (left >= right) { return true; } if (str[left] == str[right]) { return palindrome(str, left + 1, right - 1, error); } else if (error == 0) { return (palindrome(str, left + 1, right, 1) || palindrome(str,left, right -1,1)); } else { return false; } } int main() { std::string str = "abcbca"; int size = str.size() - 1; if"

    Dev S. - "#include #include bool palindrome(std::string &str, int left, int right, int error) { if (left >= right) { return true; } if (str[left] == str[right]) { return palindrome(str, left + 1, right - 1, error); } else if (error == 0) { return (palindrome(str, left + 1, right, 1) || palindrome(str,left, right -1,1)); } else { return false; } } int main() { std::string str = "abcbca"; int size = str.size() - 1; if"See full answer

    Software Engineer
    Data Structures & Algorithms
    +1 more
  • Adobe logoAsked at Adobe 
    Video answer for 'Edit distance'
    +32

    "from collections import deque def updateword(words, startword, end_word): if end_word not in words: return None # Early exit if end_word is not in the dictionary queue = deque([(start_word, 0)]) # (word, steps) visited = set([start_word]) # Keep track of visited words while queue: word, steps = queue.popleft() if word == end_word: return steps # Found the target word, return steps for i in range(len(word)): "

    叶 路. - "from collections import deque def updateword(words, startword, end_word): if end_word not in words: return None # Early exit if end_word is not in the dictionary queue = deque([(start_word, 0)]) # (word, steps) visited = set([start_word]) # Keep track of visited words while queue: word, steps = queue.popleft() if word == end_word: return steps # Found the target word, return steps for i in range(len(word)): "See full answer

    Software Engineer
    Data Structures & Algorithms
    +3 more
  • Adobe logoAsked at Adobe 
    +36

    "Was this for an entry level engineer role?"

    Yeshwanth D. - "Was this for an entry level engineer role?"See full answer

    Software Engineer
    Data Structures & Algorithms
    +4 more
  • Adobe logoAsked at Adobe 
    +38

    "public static boolean isPalindrome(String str){ boolean flag = true; int len = str.length()-1; int j = len; for(int i=0;i<=len/2;i++){ if(str.charAt(i)!=str.charAt(j--)){ flag = false; break; } } return flag; }"

    Sravanthi M. - "public static boolean isPalindrome(String str){ boolean flag = true; int len = str.length()-1; int j = len; for(int i=0;i<=len/2;i++){ if(str.charAt(i)!=str.charAt(j--)){ flag = false; break; } } return flag; }"See full answer

    Software Engineer
    Data Structures & Algorithms
    +4 more
  • Uber logoAsked at Uber 

    "public int minInsertions(String s) { int n = s.length(); int dp = new intn; for (int len = 2; len <= n; n++) { for (int i = 0; i + len - 1 < n: i++) { int j = i + len - 1; if (s.charAt(i) == s.charAt(j)) { dpi = dpi + 1; } else { dpi = 1 + Math.min(dpi + 1, dpi); } } } return dp0; } `"

    Jatin S. - "public int minInsertions(String s) { int n = s.length(); int dp = new intn; for (int len = 2; len <= n; n++) { for (int i = 0; i + len - 1 < n: i++) { int j = i + len - 1; if (s.charAt(i) == s.charAt(j)) { dpi = dpi + 1; } else { dpi = 1 + Math.min(dpi + 1, dpi); } } } return dp0; } `"See full answer

    Software Engineer
    Data Structures & Algorithms
    +1 more
  • 🧠 Want an expert answer to a question? Saving questions lets us know what content to make next.

  • Apple logoAsked at Apple 
    Video answer for 'Find the container with the maximum volume of water.'

    "int getMaxWater(vector& nums) { int n = nums.size(); int mx = INT_MIN; int i=0, j=n-1; while(i<j) { int water = (j - i) * min(nums[i], nums[j]); mx = max(mx, water); if(nums[i] < nums[j]){ i++; } else { j--; } } return mx; } `"

    Richard W. - "int getMaxWater(vector& nums) { int n = nums.size(); int mx = INT_MIN; int i=0, j=n-1; while(i<j) { int water = (j - i) * min(nums[i], nums[j]); mx = max(mx, water); if(nums[i] < nums[j]){ i++; } else { j--; } } return mx; } `"See full answer

    Data Engineer
    Data Structures & Algorithms
    +3 more
  • Video answer for 'How would you remove duplicates in a string?'
    +11

    " O(n) - characters in the string O(n) - stack def identify_adjacent(s: str, k: int) -> str: stack = [] n = len(s) for ch in s: if stack: topch, topcnt = stack[-1] if top_ch == ch: top_cnt += 1 stack[-1] = (ch, top_cnt) else: top_cnt = 1 stack.append((ch,1)) if top_cnt == k: stack.pop() else: stack.append"

    Rick E. - " O(n) - characters in the string O(n) - stack def identify_adjacent(s: str, k: int) -> str: stack = [] n = len(s) for ch in s: if stack: topch, topcnt = stack[-1] if top_ch == ch: top_cnt += 1 stack[-1] = (ch, top_cnt) else: top_cnt = 1 stack.append((ch,1)) if top_cnt == k: stack.pop() else: stack.append"See full answer

    Data Structures & Algorithms
    Coding
  • Apple logoAsked at Apple 

    "This is a geometric variation of the "Sliding Window" technique. The core challenge is converting a 2D coordinate problem into a 1D linear problem using angles. The standard approach is the Angular Sweep. By converting the Cartesian coordinates (x,y) of every tree into Polar coordinates (specifically the angle θ), we can process the trees based on the direction they lie in relative to the observer. The Strategy Coordinate Translation: Calculate the relative coordinates (Δx,Δy) for"

    Ashish D. - "This is a geometric variation of the "Sliding Window" technique. The core challenge is converting a 2D coordinate problem into a 1D linear problem using angles. The standard approach is the Angular Sweep. By converting the Cartesian coordinates (x,y) of every tree into Polar coordinates (specifically the angle θ), we can process the trees based on the direction they lie in relative to the observer. The Strategy Coordinate Translation: Calculate the relative coordinates (Δx,Δy) for"See full answer

    Software Engineer
    Data Structures & Algorithms
    +1 more
  • Adobe logoAsked at Adobe 
    +23

    "We can use dictionary to store cache items so that our read / write operations will be O(1). Each time we read or update an existing record, we have to ensure the item is moved to the back of the cache. This will allow us to evict the first item in the cache whenever the cache is full and we need to add new records also making our eviction O(1) Instead of normal dictionary, we will use ordered dictionary to store cache items. This will allow us to efficiently move items to back of the cache a"

    Alfred O. - "We can use dictionary to store cache items so that our read / write operations will be O(1). Each time we read or update an existing record, we have to ensure the item is moved to the back of the cache. This will allow us to evict the first item in the cache whenever the cache is full and we need to add new records also making our eviction O(1) Instead of normal dictionary, we will use ordered dictionary to store cache items. This will allow us to efficiently move items to back of the cache a"See full answer

    Software Engineer
    Data Structures & Algorithms
    +6 more
  • +2

    "Try not to take hints from the interviewer for solving the problem. They may provide hints but it would impact the final decision "

    Laxman kishore K. - "Try not to take hints from the interviewer for solving the problem. They may provide hints but it would impact the final decision "See full answer

    Software Engineer
    Data Structures & Algorithms
    +1 more
  • Microsoft logoAsked at Microsoft 
    Software Engineer
    Data Structures & Algorithms
    +1 more
  • Amazon logoAsked at Amazon 

    "function longestCommonPrefix(arr1, arr2) { const prefixSet = new Set(); for (let num of arr1) { let str = num.toString(); for (let i = 1; i <= str.length; i++) { prefixSet.add(str.substring(0, i)); } } let longestPrefix = ""; for (let num of arr2) { let str = num.toString(); for (let i = 1; i <= str.length; i++) { let prefix = str.substring(0, i); if (prefixSet.has(prefix)) { "

    Maykon henrique D. - "function longestCommonPrefix(arr1, arr2) { const prefixSet = new Set(); for (let num of arr1) { let str = num.toString(); for (let i = 1; i <= str.length; i++) { prefixSet.add(str.substring(0, i)); } } let longestPrefix = ""; for (let num of arr2) { let str = num.toString(); for (let i = 1; i <= str.length; i++) { let prefix = str.substring(0, i); if (prefixSet.has(prefix)) { "See full answer

    Software Engineer
    Data Structures & Algorithms
    +1 more
  • Bloomberg logoAsked at Bloomberg 

    " max Min 4, 3, 1 , 6, 7, 8 1 3 4 6 7 8 9 0 1 2 3 0 1 1 2 3 6 7 8 9 class MedianFinder{ std::priority_queue minHeap; std::priority_queue, greater> maxHeap; int numEleMaxheap = 0, numEleMinHeap = 0; public: void addNum( int n) { if(numEleMaxheap == numEleMinHeap ) { maxHeap.push(n); int maxofmaxheap = maxHeap.top(); maxHeap.pop(); "

    Ankush G. - " max Min 4, 3, 1 , 6, 7, 8 1 3 4 6 7 8 9 0 1 2 3 0 1 1 2 3 6 7 8 9 class MedianFinder{ std::priority_queue minHeap; std::priority_queue, greater> maxHeap; int numEleMaxheap = 0, numEleMinHeap = 0; public: void addNum( int n) { if(numEleMaxheap == numEleMinHeap ) { maxHeap.push(n); int maxofmaxheap = maxHeap.top(); maxHeap.pop(); "See full answer

    Software Engineer
    Data Structures & Algorithms
    +1 more
  • Amazon logoAsked at Amazon 
    +8

    "I firstly discuss the brute force approach in O(n^2) time complexity , than i moved to O(nlogn) tine complexity than i discussed the O(n) time complexity and O(n) space complexity . But interviewer want more optimised solution , in O(n) time complexity without using extra space , The solution wants O(1) space complexity i have to do changes in same array without using any space . This method is something like i have to place positive values to its original position by swapping and rest negativ"

    Anni P. - "I firstly discuss the brute force approach in O(n^2) time complexity , than i moved to O(nlogn) tine complexity than i discussed the O(n) time complexity and O(n) space complexity . But interviewer want more optimised solution , in O(n) time complexity without using extra space , The solution wants O(1) space complexity i have to do changes in same array without using any space . This method is something like i have to place positive values to its original position by swapping and rest negativ"See full answer

    Software Engineer
    Data Structures & Algorithms
    +1 more
  • "from collections import deque from typing import List def longestsubarraydifflessthan_n(nums: List[int], N: int) -> int: """ Find the length of the longest contiguous subarray such that the difference between any two elements in the subarray is less than N. Equivalent condition: max(subarray) - min(subarray) < N Approach (Optimal): Sliding window with two monotonic deques: max_d: decreasing deque of indices (front is index of current max"

    Ramachandra N. - "from collections import deque from typing import List def longestsubarraydifflessthan_n(nums: List[int], N: int) -> int: """ Find the length of the longest contiguous subarray such that the difference between any two elements in the subarray is less than N. Equivalent condition: max(subarray) - min(subarray) < N Approach (Optimal): Sliding window with two monotonic deques: max_d: decreasing deque of indices (front is index of current max"See full answer

    Software Engineer
    Data Structures & Algorithms
    +1 more
  • +8

    "Used Recursive approach to traverse the binary search tree and sum the values of the nodes that fall within the specified range [low, high]"

    Srikant V. - "Used Recursive approach to traverse the binary search tree and sum the values of the nodes that fall within the specified range [low, high]"See full answer

    Software Engineer
    Data Structures & Algorithms
    +1 more
  • +1

    " Compare alternate houses i.e for each house starting from the third, calculate the maximum money that can be stolen up to that house by choosing between: Skipping the current house and taking the maximum money stolen up to the previous house. Robbing the current house and adding its value to the maximum money stolen up to the house two steps back. package main import ( "fmt" ) // rob function calculates the maximum money a robber can steal func maxRob(nums []int) int { ln"

    VContaineers - " Compare alternate houses i.e for each house starting from the third, calculate the maximum money that can be stolen up to that house by choosing between: Skipping the current house and taking the maximum money stolen up to the previous house. Robbing the current house and adding its value to the maximum money stolen up to the house two steps back. package main import ( "fmt" ) // rob function calculates the maximum money a robber can steal func maxRob(nums []int) int { ln"See full answer

    Data Engineer
    Data Structures & Algorithms
    +4 more
  • Adobe logoAsked at Adobe 

    "Here if we breakdown each dependency [A,B] , We need to check if there a cycle in Dependency Graph. If there is cycle installation is not possible, If there is no cycle installation is possible. Steps : 1: Build the graph 2: Perform DFS based Cycle Detection 3: Check each package if those packages have cycle or not."

    Venkata rakesh M. - "Here if we breakdown each dependency [A,B] , We need to check if there a cycle in Dependency Graph. If there is cycle installation is not possible, If there is no cycle installation is possible. Steps : 1: Build the graph 2: Perform DFS based Cycle Detection 3: Check each package if those packages have cycle or not."See full answer

    Frontend Engineer
    Data Structures & Algorithms
    +1 more
  • Apple logoAsked at Apple 
    +26

    "we can use two pointer + set like maintain i,j and also insert jth character to set like while set size is equal to our window j-i+1 then maximize our answer and increase jth pointer till last index"

    Kishor J. - "we can use two pointer + set like maintain i,j and also insert jth character to set like while set size is equal to our window j-i+1 then maximize our answer and increase jth pointer till last index"See full answer

    Software Engineer
    Data Structures & Algorithms
    +4 more
  • "2 Approaches: 1) The more intuitive approach is doing a multi-source BFS from all cats and storing the distance of closest cats. Then do a dfs/bfs from rat to bread. Time Complexity: O(mn + 4^L) where L is path length, worst case L could be mn Space Complexity: O(m*n) 2) The first approach should be fine for interviews. But if they ask to optimize it further, you can use Binary Search. Problems like "Finding max of min distance" or "Finding min of max" could be usually solved by BS. "

    Karan K. - "2 Approaches: 1) The more intuitive approach is doing a multi-source BFS from all cats and storing the distance of closest cats. Then do a dfs/bfs from rat to bread. Time Complexity: O(mn + 4^L) where L is path length, worst case L could be mn Space Complexity: O(m*n) 2) The first approach should be fine for interviews. But if they ask to optimize it further, you can use Binary Search. Problems like "Finding max of min distance" or "Finding min of max" could be usually solved by BS. "See full answer

    Software Engineer
    Data Structures & Algorithms
    +1 more
Showing 1-20 of 264