Data Structures & Algorithms Interview Questions

Review this list of 241 data structures & algorithms interview questions and answers verified by hiring managers and candidates.
  • Meta (Facebook) logoAsked at Meta (Facebook) 
    Software Engineer
    Data Structures & Algorithms
    +1 more
  • Meta (Facebook) logoAsked at Meta (Facebook) 
    Video answer for 'Find the common ancestors in a tree.'
    Machine Learning Engineer
    Data Structures & Algorithms
    +1 more
  • Salesforce logoAsked at Salesforce 

    "Bitshift the number to the right and keep track of the 1's you encounter. If you bitshift it completely and only encounter one 1, it is a power of two."

    Nils G. - "Bitshift the number to the right and keep track of the 1's you encounter. If you bitshift it completely and only encounter one 1, it is a power of two."See full answer

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

    Permutations

    IDE
    Medium

    "function permute(nums) { if (nums.length <= 1) { return [nums]; } const prevPermutations = permute(nums.slice(0, nums.length-1)); const currentNum = nums[nums.length-1]; const permutations = new Set(); for (let prev of prevPermutations) { for (let i=0; i < prev.length; i++) { permutations.add([...prev.slice(0, i), currentNum, ...prev.slice(i)]); } permutations.add([...prev, currentNum]); } return [...permutations]"

    Tiago R. - "function permute(nums) { if (nums.length <= 1) { return [nums]; } const prevPermutations = permute(nums.slice(0, nums.length-1)); const currentNum = nums[nums.length-1]; const permutations = new Set(); for (let prev of prevPermutations) { for (let i=0; i < prev.length; i++) { permutations.add([...prev.slice(0, i), currentNum, ...prev.slice(i)]); } permutations.add([...prev, currentNum]); } return [...permutations]"See full answer

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

  • Asked at Confluent 
    Software Engineer
    Data Structures & Algorithms
    +1 more
  • "Construct a min-heap either inplace, or by making a copy of the array and then applying heapify on that copy. This is done in O(n) time. Maintain two zero-initialised variables - sum and count. Keep popping off the heap while sum < k, and update count. In the worst case you will have to do n pops, and each pop is O(log n), so the algorithm would take O(n log n) in total. Space complexity depends on whether you're allowed to modify inplace or not, so either O(1) or O(n) respectively."

    Anonymous Wolf - "Construct a min-heap either inplace, or by making a copy of the array and then applying heapify on that copy. This is done in O(n) time. Maintain two zero-initialised variables - sum and count. Keep popping off the heap while sum < k, and update count. In the worst case you will have to do n pops, and each pop is O(log n), so the algorithm would take O(n log n) in total. Space complexity depends on whether you're allowed to modify inplace or not, so either O(1) or O(n) respectively."See full answer

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

    "Assuming that trades will have information like trade_type buy or sell trade_price with these tuples, one can iterate over each trade while maintaining a stack which maintains all the open buy trades. If we encounter a sell trade then we pop one element make it a buy/sell pair and calculate the profit/loss for that pair. Moreover, keep adding pair-wise profit/loss to calculate overall profit as we continue iterating over trades. At the end print pairs and their profit/loss along with"

    Parth S. - "Assuming that trades will have information like trade_type buy or sell trade_price with these tuples, one can iterate over each trade while maintaining a stack which maintains all the open buy trades. If we encounter a sell trade then we pop one element make it a buy/sell pair and calculate the profit/loss for that pair. Moreover, keep adding pair-wise profit/loss to calculate overall profit as we continue iterating over trades. At the end print pairs and their profit/loss along with"See full answer

    Data Structures & Algorithms
    Coding
    +1 more
  • +4

    "def getdifferentnumber(arr): \# arr of nonnegative ints \# find the smallest non negative int that isn't in the array MAX_INT = 2^31-1 \# not allowed to modify the input arr seen = [] smallest = 0 for i in arr: if smallest in arr: smallest += 1 seen.append(i) return smallest"

    Anonymous Owl - "def getdifferentnumber(arr): \# arr of nonnegative ints \# find the smallest non negative int that isn't in the array MAX_INT = 2^31-1 \# not allowed to modify the input arr seen = [] smallest = 0 for i in arr: if smallest in arr: smallest += 1 seen.append(i) return smallest"See full answer

    Data Structures & Algorithms
    Coding
  • Adobe logoAsked at Adobe 

    "static boolean sudokuSolve(char board) { return sudokuSolve(board, 0, 0); } static boolean sudokuSolve(char board, int r, int c) { if(c>=board[0].length) { r=r+1; c=0; } if(r>=board.length) return true; if(boardr=='.') { for(int num=1; num<=9; num++) { boardr=(char)('0' + num); if(isValidPosition(board, r, c)) { if(sudokuSolve(board, r, c+1)) return true; } boardr='.'; } } else { return sudokuSolve(board, r, c+1); } return false; } static boolean isValidPosition(char b"

    Divya R. - "static boolean sudokuSolve(char board) { return sudokuSolve(board, 0, 0); } static boolean sudokuSolve(char board, int r, int c) { if(c>=board[0].length) { r=r+1; c=0; } if(r>=board.length) return true; if(boardr=='.') { for(int num=1; num<=9; num++) { boardr=(char)('0' + num); if(isValidPosition(board, r, c)) { if(sudokuSolve(board, r, c+1)) return true; } boardr='.'; } } else { return sudokuSolve(board, r, c+1); } return false; } static boolean isValidPosition(char b"See full answer

    Software Engineer
    Data Structures & Algorithms
    +4 more
  • +3

    "def flatten_dictionary(dictionary): \# return a flattened dictionary - int/string/another dictionary values \# if the key is empty, exclude from the output \# concat using a "." btwn them \# add to res which is { "key.a.b.etc": "value" } \# iterate through the key value pairs \# while there is a key value pair in the value \# continue going through that, until the value is an int/string flatDic = {} flatDicHelper("", dictionary, flatDic) print(flatDic) return flatDic def flatDicHelper(initialKey"

    Anonymous Owl - "def flatten_dictionary(dictionary): \# return a flattened dictionary - int/string/another dictionary values \# if the key is empty, exclude from the output \# concat using a "." btwn them \# add to res which is { "key.a.b.etc": "value" } \# iterate through the key value pairs \# while there is a key value pair in the value \# continue going through that, until the value is an int/string flatDic = {} flatDicHelper("", dictionary, flatDic) print(flatDic) return flatDic def flatDicHelper(initialKey"See full answer

    Data Structures & Algorithms
    Coding
  • +4

    "Using a DFS approach, computing all the distances from typing import List from collections import deque def shortestCellPath(grid: List[List[int]], sr: int, sc: int, tr: int, tc: int) -> int: if sr == tr and sc == tc: return 0 nRows = len(grid) nCols = len(grid[0]) distances = [] stack = deque([(sr, sc, 0)]) visitedSet = set() while stack: nodeR, nodeC, nodeDist = stack.pop() if gridnodeR == 0 or (nodeR, nodeC) in visited"

    Gabriele G. - "Using a DFS approach, computing all the distances from typing import List from collections import deque def shortestCellPath(grid: List[List[int]], sr: int, sc: int, tr: int, tc: int) -> int: if sr == tr and sc == tc: return 0 nRows = len(grid) nCols = len(grid[0]) distances = [] stack = deque([(sr, sc, 0)]) visitedSet = set() while stack: nodeR, nodeC, nodeDist = stack.pop() if gridnodeR == 0 or (nodeR, nodeC) in visited"See full answer

    Data Structures & Algorithms
    Coding
  • Apple logoAsked at Apple 

    "Sorting is a technique to arrange data in either increasing order or decreasing order, and, the function that implements this functionality is called sort function"

    Farhan -. - "Sorting is a technique to arrange data in either increasing order or decreasing order, and, the function that implements this functionality is called sort function"See full answer

    Software Engineer
    Data Structures & Algorithms
    +1 more
  • Meta (Facebook) logoAsked at Meta (Facebook) 
    Machine Learning Engineer
    Data Structures & Algorithms
    +1 more
  • Meta (Facebook) logoAsked at Meta (Facebook) 

    "sum of continuous subarray and keep checking if arr[i]==arr[j]. if true increase count;"

    Rishabh R. - "sum of continuous subarray and keep checking if arr[i]==arr[j]. if true increase count;"See full answer

    Data Structures & Algorithms
    Coding
    +1 more
  • Adobe logoAsked at Adobe 
    +7

    "from typing import List def traprainwater(height: List[int]) -> int: if not height: return 0 l, r = 0, len(height) - 1 leftMax, rightMax = height[l], height[r] res = 0 while l < r: if leftMax < rightMax: l += 1 leftMax = max(leftMax, height[l]) res += leftMax - height[l] else: r -= 1 rightMax = max(rightMax, height[r]) "

    Anonymous Roadrunner - "from typing import List def traprainwater(height: List[int]) -> int: if not height: return 0 l, r = 0, len(height) - 1 leftMax, rightMax = height[l], height[r] res = 0 while l < r: if leftMax < rightMax: l += 1 leftMax = max(leftMax, height[l]) res += leftMax - height[l] else: r -= 1 rightMax = max(rightMax, height[r]) "See full answer

    Software Engineer
    Data Structures & Algorithms
    +4 more
  • Meta (Facebook) logoAsked at Meta (Facebook) 
    Machine Learning Engineer
    Data Structures & Algorithms
    +1 more
  • "def findAlibaba(countOfRooms, strategy): #countofrooms: num rooms #listRooms rooms to look for alibabba possiblePlaces = [] #initialize rooms for i in range(countOfRooms): possiblePlaces.append(True) for i in range(len(strategy)): roomToCheck = strategy[i] #Room is marked as unavailable possiblePlaces[roomToCheck] = False #Next day calculatins nextDayPlaces = [] for j in range(countOfRooms): nextDayPla"

    JOBHUNTER - "def findAlibaba(countOfRooms, strategy): #countofrooms: num rooms #listRooms rooms to look for alibabba possiblePlaces = [] #initialize rooms for i in range(countOfRooms): possiblePlaces.append(True) for i in range(len(strategy)): roomToCheck = strategy[i] #Room is marked as unavailable possiblePlaces[roomToCheck] = False #Next day calculatins nextDayPlaces = [] for j in range(countOfRooms): nextDayPla"See full answer

    Engineering Manager
    Data Structures & Algorithms
    +1 more
Showing 141-160 of 241