Data Structures & Algorithms Interview Questions

Review this list of 226 data structures & algorithms interview questions and answers verified by hiring managers and candidates.
  • Google logoAsked at Google 
    +3

    "import time class Task: def init\(self, description, interval=None): self.description = description self.interval = interval self.next_run = time.time() class SimpleTaskScheduler: def init\(self): self.tasks = [] def add_task(self, description, interval=None): self.tasks.append(Task(description, interval)) def run(self, duration=60): end_time = time.time() + duration while time.time() < end_time: curr"

    Yash N. - "import time class Task: def init\(self, description, interval=None): self.description = description self.interval = interval self.next_run = time.time() class SimpleTaskScheduler: def init\(self): self.tasks = [] def add_task(self, description, interval=None): self.tasks.append(Task(description, interval)) def run(self, duration=60): end_time = time.time() + duration while time.time() < end_time: curr"See full answer

    Machine Learning Engineer
    Data Structures & Algorithms
    +1 more
  • Adobe logoAsked at Adobe 
    +37

    "#include // Naive method to find a pair in an array with a given sum void findPair(int nums[], int n, int target) { // consider each element except the last for (int i = 0; i < n - 1; i++) { // start from the i'th element until the last element for (int j = i + 1; j < n; j++) { // if the desired sum is found, print it if (nums[i] + nums[j] == target) { printf("Pair found (%d, %d)\n", nums[i], nums[j]); return; } } } // we reach here if the pair is not found printf("Pair not found"); } "

    Gundala tarun,cse2020 V. - "#include // Naive method to find a pair in an array with a given sum void findPair(int nums[], int n, int target) { // consider each element except the last for (int i = 0; i < n - 1; i++) { // start from the i'th element until the last element for (int j = i + 1; j < n; j++) { // if the desired sum is found, print it if (nums[i] + nums[j] == target) { printf("Pair found (%d, %d)\n", nums[i], nums[j]); return; } } } // we reach here if the pair is not found printf("Pair not found"); } "See full answer

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

    "public static void sortBinaryArray(int[] array) { int len = array.length; int[] res = new int[len]; int r=len-1; for (int value : array) { if(value==1){ res[r]= 1; r--; } } System.out.println(Arrays.toString(res)); } `"

    Nitin P. - "public static void sortBinaryArray(int[] array) { int len = array.length; int[] res = new int[len]; int r=len-1; for (int value : array) { if(value==1){ res[r]= 1; r--; } } System.out.println(Arrays.toString(res)); } `"See full answer

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

    "Should this question be BST, not just BT? Otherwise it would not be possible to reconstruct the tree solely based on the array regardless of its order"

    TreeOfWisdom - "Should this question be BST, not just BT? Otherwise it would not be possible to reconstruct the tree solely based on the array regardless of its order"See full answer

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

    "Problem Statement: The Fibonacci sequence is defined as F(n) = F(n-1) + F(n-2) with F(0) = 1 and F(1) = 1. The solution is given in the problem statement itself. If the value of n = 0, return 1. If the value of n = 1, return 1. Otherwise, return the sum of data at (n - 1) and (n - 2). Explanation: The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones, typically starting with 0 and 1. Java Solution: public static int fib(int n"

    Rishi G. - "Problem Statement: The Fibonacci sequence is defined as F(n) = F(n-1) + F(n-2) with F(0) = 1 and F(1) = 1. The solution is given in the problem statement itself. If the value of n = 0, return 1. If the value of n = 1, return 1. Otherwise, return the sum of data at (n - 1) and (n - 2). Explanation: The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones, typically starting with 0 and 1. Java Solution: public static int fib(int n"See full answer

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

  • Meta (Facebook) logoAsked at Meta (Facebook) 

    "int[] sqSorted(int[] nums) { int i = 0, j = nums.length-1; int k = nums.length-1; int[] sqs = new int[nums.length]; while(i n1) { sqs[k--] = n2; j--; } else { sqs[k--] = n1; i++; } } for(int n: sqs) System.out.println(n); return sqs; }"

    Mahaboob P. - "int[] sqSorted(int[] nums) { int i = 0, j = nums.length-1; int k = nums.length-1; int[] sqs = new int[nums.length]; while(i n1) { sqs[k--] = n2; j--; } else { sqs[k--] = n1; i++; } } for(int n: sqs) System.out.println(n); return sqs; }"See full answer

    Data Engineer
    Data Structures & Algorithms
    +2 more
  • DoorDash logoAsked at DoorDash 

    "Binary Search on the array and after than compare the numbers at low and the high pointers whichever is closest is the answer. Because after the binary search low will be pointing to a number which is immediate greater than x and high will be pointing to a number which is immediate lesser than x. int low = 0; int high = n-1; while(low <= high){ int mid = (low + high) / 2; if(x == arr[mid]) return mid; //if x is already present then it will be the closest else if(x < arr[mid]) high"

    Shashwat K. - "Binary Search on the array and after than compare the numbers at low and the high pointers whichever is closest is the answer. Because after the binary search low will be pointing to a number which is immediate greater than x and high will be pointing to a number which is immediate lesser than x. int low = 0; int high = n-1; while(low <= high){ int mid = (low + high) / 2; if(x == arr[mid]) return mid; //if x is already present then it will be the closest else if(x < arr[mid]) high"See full answer

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

    "def validateIP(ip): """ @param ip: str @return: bool """ \# ip needs to be in X.X.X.X \# X is from 0 to 255 \# split the ip at "." split = ip.split('.') if (len(split) != 4): return False for number in split: if (int(number) 255): return False return True"

    Anonymous Owl - "def validateIP(ip): """ @param ip: str @return: bool """ \# ip needs to be in X.X.X.X \# X is from 0 to 255 \# split the ip at "." split = ip.split('.') if (len(split) != 4): return False for number in split: if (int(number) 255): return False return True"See full answer

    Data Structures & Algorithms
    Coding
  • "class Solution: def missingNumber(self, nums: list[int]) -> int: Sorting approach n = len(nums) s = n*(n+1)//2 r = s - sum(nums) return self.r l = [3,0,1] print(missingNumber(l))"

    Rohit B. - "class Solution: def missingNumber(self, nums: list[int]) -> int: Sorting approach n = len(nums) s = n*(n+1)//2 r = s - sum(nums) return self.r l = [3,0,1] print(missingNumber(l))"See full answer

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

    "static int[] sortKMessedArray(int[] inputarr, int k) { for (int i= 1; i= 0 ) { if (curval < inputarr[j] ) { inputarr[j+1] = inputarr[j]; j-- ; } } //inputarr[i] = inputarr[j] ; inputarr[j+1] = curval; } return inputarr; }"

    Dm - "static int[] sortKMessedArray(int[] inputarr, int k) { for (int i= 1; i= 0 ) { if (curval < inputarr[j] ) { inputarr[j+1] = inputarr[j]; j-- ; } } //inputarr[i] = inputarr[j] ; inputarr[j+1] = curval; } return inputarr; }"See full answer

    Data Structures & Algorithms
    Coding
  • "Example: bucket A: 3 liters capacity bucket B: 5 liters capacity goal: 4 liters You are asked to print the logical sequence to get to the 4 liters of water in one bucket. Follow up: How would you solve the problem if you have more than 2 buckets of water?"

    B. T. - "Example: bucket A: 3 liters capacity bucket B: 5 liters capacity goal: 4 liters You are asked to print the logical sequence to get to the 4 liters of water in one bucket. Follow up: How would you solve the problem if you have more than 2 buckets of water?"See full answer

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

    "I was able to provide the optimal approach and coded it up"

    Anonymous Wasp - "I was able to provide the optimal approach and coded it up"See full answer

    Data Engineer
    Data Structures & Algorithms
    +2 more
  • Adobe logoAsked at Adobe 
    Video answer for 'Solve John Conway's "Game of Life".'
    Software Engineer
    Data Structures & Algorithms
    +2 more
  • Data Structures & Algorithms
    Coding
  • Oracle logoAsked at Oracle 
    +1

    "I think sliding window will work here and it is the most optimized approach to solve this question."

    Gaurav K. - "I think sliding window will work here and it is the most optimized approach to solve this question."See full answer

    Software Engineer
    Data Structures & Algorithms
    +1 more
  • Adobe logoAsked at Adobe 
    Video answer for 'Merge k sorted linked lists.'
    +6

    "A much better solution than the one in the article, below: It looks like the ones writing articles here in Javascript do not understand the time/space complexity of javascript methods. shift, splice, sort, etc... In the solution article you have a shift and a sort being done inside a while, that is, the multiplication of Ns. My solution, below, iterates through the list once and then sorts it, separately. It´s O(N+Log(N)) class ListNode { constructor(val = 0, next = null) { th"

    Guilherme F. - "A much better solution than the one in the article, below: It looks like the ones writing articles here in Javascript do not understand the time/space complexity of javascript methods. shift, splice, sort, etc... In the solution article you have a shift and a sort being done inside a while, that is, the multiplication of Ns. My solution, below, iterates through the list once and then sorts it, separately. It´s O(N+Log(N)) class ListNode { constructor(val = 0, next = null) { th"See full answer

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

    "To determine if a graph is not a tree, you can check for the following conditions: Presence of cycles: A graph is not a tree if it contains cycles. In a tree, there should be exactly one unique path between any two vertices. If you can find a cycle in the graph, it cannot be a tree. Insufficient number of edges: A tree with N vertices will have exactly N-1 edges. If the graph has fewer or more than N-1 edges, then it is not a tree. Disconnected components: A tree is a connected graph, m"

    Vaibhav C. - "To determine if a graph is not a tree, you can check for the following conditions: Presence of cycles: A graph is not a tree if it contains cycles. In a tree, there should be exactly one unique path between any two vertices. If you can find a cycle in the graph, it cannot be a tree. Insufficient number of edges: A tree with N vertices will have exactly N-1 edges. If the graph has fewer or more than N-1 edges, then it is not a tree. Disconnected components: A tree is a connected graph, m"See full answer

    Software Engineer
    Data Structures & Algorithms
    +2 more
  • Data Structures & Algorithms
    Coding
  • Google logoAsked at Google 
    +6

    "function areSentencesSimilar(sentence1, sentence2, similarPairs) { if (sentence1.length !== sentence2.length) return false; for (let i=0; i (w1 === word1 && !visited.has(w2)) || (w2 === word1 && !visited.has(w1))); if (!edge) { "

    Tiago R. - "function areSentencesSimilar(sentence1, sentence2, similarPairs) { if (sentence1.length !== sentence2.length) return false; for (let i=0; i (w1 === word1 && !visited.has(w2)) || (w2 === word1 && !visited.has(w1))); if (!edge) { "See full answer

    Software Engineer
    Data Structures & Algorithms
    +2 more
Showing 81-100 of 226