Data Structures & Algorithms Interview Questions

Review this list of 226 data structures & algorithms interview questions and answers verified by hiring managers and candidates.
  • Adobe logoAsked at Adobe 
    Video answer for 'Edit distance'
    +16

    "I am not clear on the relevance of the "list of valid words" to the original problem statement. It seems to have led the candidate to build a graph. Navigating a graph of all possible paths to the target word is computationally expensive, plus unnecessary as you are navigating the world of "possible" words, whereas to edit characters in a word, you don't necessarily need the intermediate words to be valid. Overall the answer to this problem seems to be to use dynamic programming. It would be g"

    PracticalCoder - "I am not clear on the relevance of the "list of valid words" to the original problem statement. It seems to have led the candidate to build a graph. Navigating a graph of all possible paths to the target word is computationally expensive, plus unnecessary as you are navigating the world of "possible" words, whereas to edit characters in a word, you don't necessarily need the intermediate words to be valid. Overall the answer to this problem seems to be to use dynamic programming. It would be g"See full answer

    Software Engineer
    Data Structures & Algorithms
    +3 more
  • "Max distance can be between cat to rat or cat to bread. lets say it is x. I tried to find a path between rat and bread using DFS for every distance from cat(max upto x)."

    Pankaj G. - "Max distance can be between cat to rat or cat to bread. lets say it is x. I tried to find a path between rat and bread using DFS for every distance from cat(max upto x)."See full answer

    Software Engineer
    Data Structures & Algorithms
    +1 more
  • 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
  • Adobe logoAsked at Adobe 
    +28

    "Reversing a linked list is a very popular question. We have two approaches to reverse the linked list: Iterative approach and recursion approach. Iterative approach (JavaScript) function reverseLL(head){ if(head === null) return head; let prv = null; let next = null; let cur = head; while(cur){ next = cur.next; //backup cur.next = prv; prv = cur; cur = next; } head = prv; return head; } Recursion Approach (JS) function reverseLLByRecursion("

    Satyam S. - "Reversing a linked list is a very popular question. We have two approaches to reverse the linked list: Iterative approach and recursion approach. Iterative approach (JavaScript) function reverseLL(head){ if(head === null) return head; let prv = null; let next = null; let cur = head; while(cur){ next = cur.next; //backup cur.next = prv; prv = cur; cur = next; } head = prv; return head; } Recursion Approach (JS) function reverseLLByRecursion("See full answer

    Software Engineer
    Data Structures & Algorithms
    +4 more
  • "Batch Packing Problem In Amazon’s massive warehouse inventory, there are different types of products. You are given an array products of size n, where products[i] represents the number of items of product type i. These products need to be packed into batches for shipping. The batch packing must adhere to the following conditions: No two items in the same batch can be of the same product type. The number of items packed in the current batch must be strictly greater than the number pack"

    Anonymous Goat - "Batch Packing Problem In Amazon’s massive warehouse inventory, there are different types of products. You are given an array products of size n, where products[i] represents the number of items of product type i. These products need to be packed into batches for shipping. The batch packing must adhere to the following conditions: No two items in the same batch can be of the same product type. The number of items packed in the current batch must be strictly greater than the number pack"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.

  • +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
  • Adobe logoAsked at Adobe 
    +25

    "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
  • 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
  • " 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
  • Video answer for 'How would you remove duplicates in a string?'
    +7

    " 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
  • Meta (Facebook) logoAsked at Meta (Facebook) 
    +2

    "Implemented a recursive function which returns the length of the list so far. when the returned value equals k + 1 , assign current.next = current.next.next. If I made it back to the head return root.next as the new head of the linked list."

    דניאל ר. - "Implemented a recursive function which returns the length of the list so far. when the returned value equals k + 1 , assign current.next = current.next.next. If I made it back to the head return root.next as the new head of the linked list."See full answer

    Machine Learning Engineer
    Data Structures & Algorithms
    +2 more
  • "Use Dutch National Flag Algorithm to solve the problem"

    Sireesha R. - "Use Dutch National Flag Algorithm to solve the problem"See full answer

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

    "#include #include #include using namespace std; vector diff(const vector& A, const vector& B) { unordered_set elements; vector result; for (const auto& element : A) { elements.insert(element); } for (const auto& element : B) { if (elements.find(element) == elements.end()) { result.push_back(element); } else { elements.erase(element); } } for"

    Warrenbuff - "#include #include #include using namespace std; vector diff(const vector& A, const vector& B) { unordered_set elements; vector result; for (const auto& element : A) { elements.insert(element); } for (const auto& element : B) { if (elements.find(element) == elements.end()) { result.push_back(element); } else { elements.erase(element); } } for"See full answer

    Data Structures & Algorithms
    Coding
  • +2

    "Started off with brute force then optimized solution"

    Nikhil R. - "Started off with brute force then optimized solution"See full answer

    Machine Learning Engineer
    Data Structures & Algorithms
    +2 more
  • Adobe logoAsked at Adobe 
    +15

    "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
    +5 more
  • Meta (Facebook) logoAsked at Meta (Facebook) 
    Software Engineer
    Data Structures & Algorithms
    +1 more
  • Adobe logoAsked at Adobe 

    "Use a representative of each, e.g. sort the string and add it to the value of a hashmap> where we put all the words that belong to the same anagram together."

    Gaston B. - "Use a representative of each, e.g. sort the string and add it to the value of a hashmap> where we put all the words that belong to the same anagram together."See full answer

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

    "class Node: def init(self, value): self.value = value self.children = [] def inorder_traversal(root): if not root: return [] result = [] n = len(root.children) for i in range(n): result.extend(inorder_traversal(root.children[i])) if i == n // 2: result.append(root.value) if n == 0: result.append(root.value) return result Example usage: root = Node(1) child1 = Node(2) chil"

    Teddy Y. - "class Node: def init(self, value): self.value = value self.children = [] def inorder_traversal(root): if not root: return [] result = [] n = len(root.children) for i in range(n): result.extend(inorder_traversal(root.children[i])) if i == n // 2: result.append(root.value) if n == 0: result.append(root.value) return result Example usage: root = Node(1) child1 = Node(2) chil"See full answer

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

    "Would be better to adjust resolution in the video player directly."

    Anonymous Prawn - "Would be better to adjust resolution in the video player directly."See full answer

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