Data Structures & Algorithms Interview Questions

Review this list of 237 data structures & algorithms interview questions and answers verified by hiring managers and candidates.
  • Microsoft logoAsked at Microsoft 

    "Sorted the array and stored the minimum difference in a variable and then traversed the array for the pairs having minimum difference"

    Aashka C. - "Sorted the array and stored the minimum difference in a variable and then traversed the array for the pairs having minimum difference"See full answer

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

    "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
  • Microsoft logoAsked at Microsoft 

    "int reverse(int x) { int rev = 0; bool isNegative = false; if(x 0){ int r = x % 10; rev = rev * 10 + r; x = x / 10; } if(isNegative){ return -rev; } return rev; } `"

    Nilay B. - "int reverse(int x) { int rev = 0; bool isNegative = false; if(x 0){ int r = x % 10; rev = rev * 10 + r; x = x / 10; } if(isNegative){ return -rev; } return rev; } `"See full answer

    Software Engineer
    Data Structures & Algorithms
    +1 more
  • "I would assume that this is similar to an intervals question. Meeting Rooms II (https://www.lintcode.com/problem/919/?fromId=203&_from=collection) on Leetcode seems like the closest comparison, it's a premium question so I linked Lintcode. I'm assuming that we also need to just return the minimum number of cars used. You need to sort for the most optimal solution, so you're constrained by an O(nlogn) time complexity. So any sorting solution could work (using a heap, sorting the array input arra"

    Sohum S. - "I would assume that this is similar to an intervals question. Meeting Rooms II (https://www.lintcode.com/problem/919/?fromId=203&_from=collection) on Leetcode seems like the closest comparison, it's a premium question so I linked Lintcode. I'm assuming that we also need to just return the minimum number of cars used. You need to sort for the most optimal solution, so you're constrained by an O(nlogn) time complexity. So any sorting solution could work (using a heap, sorting the array input arra"See full answer

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

    "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
  • 🧠 Want an expert answer to a question? Saving questions lets us know what content to make next.

  • Meta (Facebook) logoAsked at Meta (Facebook) 
    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
  • "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
  • "find total sum. assign that to rightsum traverse from left to right: keep updating left sum and right sum, when they match return the index. else if you reach end return -1 or not found"

    Rahul J. - "find total sum. assign that to rightsum traverse from left to right: keep updating left sum and right sum, when they match return the index. else if you reach end return -1 or not found"See full answer

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

    "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
  • Meta (Facebook) logoAsked at Meta (Facebook) 
    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
  • Video answer for 'How would you remove duplicates in a string?'
    +8

    " 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
  • 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
  • "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
  • " 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
  • Machine Learning Engineer
    Data Structures & Algorithms
    +1 more
  • Adobe logoAsked at Adobe 
    +16

    "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
  • "/* You are with your friends in a castle, where there are multiple rooms named after flowers. Some of the rooms contain treasures - we call them the treasure rooms. Each room contains a single instruction that tells you which room to go to next. * instructions1 and treasurerooms_1 * lily* --------- daisy sunflower | | | v v v jasmin --> tulip* violet* ----> rose* --> ^ | ^ ^ | "

    Azeezat R. - "/* You are with your friends in a castle, where there are multiple rooms named after flowers. Some of the rooms contain treasures - we call them the treasure rooms. Each room contains a single instruction that tells you which room to go to next. * instructions1 and treasurerooms_1 * lily* --------- daisy sunflower | | | v v v jasmin --> tulip* violet* ----> rose* --> ^ | ^ ^ | "See full answer

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

    "#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
Showing 1-20 of 237