Data Structures & Algorithms Interview Questions

Review this list of 241 data structures & algorithms interview questions and answers verified by hiring managers and candidates.
  • " function diffBetweenTwoStrings(source, target) { /** @param source: string @param target: string @return: string[] */ let dp = new Array(source.length+1).fill().map(() => Array(target.length+1).fill(0)) for (let i = source.length; i>= 0; i--) { for (let j = target.length; j>= 0; j--) { if (i === source.length) { dpi = target.length - j } else if (j === target.length) { dpi = sou"

    Matthew K. - " function diffBetweenTwoStrings(source, target) { /** @param source: string @param target: string @return: string[] */ let dp = new Array(source.length+1).fill().map(() => Array(target.length+1).fill(0)) for (let i = source.length; i>= 0; i--) { for (let j = target.length; j>= 0; j--) { if (i === source.length) { dpi = target.length - j } else if (j === target.length) { dpi = sou"See full answer

    Data Structures & Algorithms
    Coding
  • New York Times logoAsked at New York Times 

    "input = [ {"topic": 1, "chapter": 1, "section": 1}, {"topic": 2, "chapter": 2, "section": 1}, {"topic": 3, "chapter": 2, "section": 2}, {"topic": 4, "chapter": 1, "section": 1}, {"topic": 5, "chapter": 1, "section": 1}, {"topic": 6, "chapter": 2, "section": 2}, {"topic": 7, "chapter": 2, "section": 2}, {"topic": 8, "chapter": 2, "section": 3}, ] expected_output = [ {'chapter': 1, 'sections': [ {'section': 1, 'topics': [ {'top"

    Anonymous Unicorn - "input = [ {"topic": 1, "chapter": 1, "section": 1}, {"topic": 2, "chapter": 2, "section": 1}, {"topic": 3, "chapter": 2, "section": 2}, {"topic": 4, "chapter": 1, "section": 1}, {"topic": 5, "chapter": 1, "section": 1}, {"topic": 6, "chapter": 2, "section": 2}, {"topic": 7, "chapter": 2, "section": 2}, {"topic": 8, "chapter": 2, "section": 3}, ] expected_output = [ {'chapter': 1, 'sections': [ {'section': 1, 'topics': [ {'top"See full answer

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

    "if decreasing arr, start from end and keep checking if next element increases by 1 or not. wherever not, put that value there."

    Rishabh R. - "if decreasing arr, start from end and keep checking if next element increases by 1 or not. wherever not, put that value there."See full answer

    Data Structures & Algorithms
    Coding
    +1 more
  • "boolean isMatch(String s, String p) { int i=0; int j=0; int sID=-1; int prevM=-1; while(i<s.length()){ if(j<p.length() && (s.charAt(i)==p.charAt(j) || p.charAt(j)=='?')){ i++; j++; }else if(j<p.length() && p.charAt(j)=='*'){ sID=j; prevM=i; j++; }else if(sID!=-1){ j=sID+1; prevM++; i=prevM; }else{ return false; } } while(j<p.length() && p.charAt(j)=='*') j++; if(i!=s.length() || j!=p.leng"

    Ravi C. - "boolean isMatch(String s, String p) { int i=0; int j=0; int sID=-1; int prevM=-1; while(i<s.length()){ if(j<p.length() && (s.charAt(i)==p.charAt(j) || p.charAt(j)=='?')){ i++; j++; }else if(j<p.length() && p.charAt(j)=='*'){ sID=j; prevM=i; j++; }else if(sID!=-1){ j=sID+1; prevM++; i=prevM; }else{ return false; } } while(j<p.length() && p.charAt(j)=='*') j++; if(i!=s.length() || j!=p.leng"See full answer

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

    "simply check its size if the size if the size is greater than n then yes it has duplicate"

    Kunal kumar S. - "simply check its size if the size if the size is greater than n then yes it has duplicate"See full answer

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

  • Goldman Sachs logoAsked at Goldman Sachs 

    "standard answer for this."

    Shar N. - "standard answer for this."See full answer

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

    " This is mostly correct and fairly fast. My code has a bug somewhere where it fails on cases like the last case, where there are negative number on both ends of the array and the sums . from collections import deque debug = True # False def prdbg(*x): global debug debug = True # False if debug: print(x) else: return def max_sum(arr, start, end): if type(arr) == type(''' "

    Nathan B. - " This is mostly correct and fairly fast. My code has a bug somewhere where it fails on cases like the last case, where there are negative number on both ends of the array and the sums . from collections import deque debug = True # False def prdbg(*x): global debug debug = True # False if debug: print(x) else: return def max_sum(arr, start, end): if type(arr) == type(''' "See full answer

    Data Structures & Algorithms
    Coding
  • Apple logoAsked at Apple 
    Software Engineer
    Data Structures & Algorithms
    +1 more
  • JP Morgan Chase logoAsked at JP Morgan Chase 
    Software Engineer
    Data Structures & Algorithms
    +1 more
  • "ArrayList allows constant time access (O(1)) to elements using their index because it uses a dynamic array internally, whereas LinkedList requires traversal from the head node, resulting in linear time complexity (O(n))."

    Aziz V. - "ArrayList allows constant time access (O(1)) to elements using their index because it uses a dynamic array internally, whereas LinkedList requires traversal from the head node, resulting in linear time complexity (O(n))."See full answer

    Software Engineer
    Data Structures & Algorithms
    +1 more
  • Asked at Confluent 
    Software Engineer
    Data Structures & Algorithms
    +1 more
  • Snap logoAsked at Snap 

    "I'd translated this decimal number into binary form and if it looks like 1000 or 10 or 1 - one leading 1 and others are zeros - than return 1 else return 0 "

    Анвар А. - "I'd translated this decimal number into binary form and if it looks like 1000 or 10 or 1 - one leading 1 and others are zeros - than return 1 else return 0 "See full answer

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

    "function isPalindrome(s, start, end) { while (s[start] === s[end] && end >= start) { start++; end--; } return end <= start; } function longestPalindromicSubstring(s) { let longestPalindrome = ''; for (let i=0; i < s.length; i++) { let j = s.length-1; while (s[i] !== s[j] && i <= j) { j--; } if (s[i] === s[j]) { if (isPalindrome(s, i, j)) { const validPalindrome = s.substring(i, j+1"

    Tiago R. - "function isPalindrome(s, start, end) { while (s[start] === s[end] && end >= start) { start++; end--; } return end <= start; } function longestPalindromicSubstring(s) { let longestPalindrome = ''; for (let i=0; i < s.length; i++) { let j = s.length-1; while (s[i] !== s[j] && i <= j) { j--; } if (s[i] === s[j]) { if (isPalindrome(s, i, j)) { const validPalindrome = s.substring(i, j+1"See full answer

    Data Engineer
    Data Structures & Algorithms
    +3 more
  • "import java.util.*; public class NetworkTopology { public int topologytype(int N, int M, int[] input3, int[] input4) { if (M != N - 1 && M != N) return -1; // Fast check for invalid cases int[] degree = new int[N + 1]; // Degree of each node (1-based indexing) // Build the degree array for (int i = 0; i < M; i++) { degree[input3[i]]++; degree[input4[i]]++; } // Check for Bus Topology boolean isBus = (M"

    Alessandro R. - "import java.util.*; public class NetworkTopology { public int topologytype(int N, int M, int[] input3, int[] input4) { if (M != N - 1 && M != N) return -1; // Fast check for invalid cases int[] degree = new int[N + 1]; // Degree of each node (1-based indexing) // Build the degree array for (int i = 0; i < M; i++) { degree[input3[i]]++; degree[input4[i]]++; } // Check for Bus Topology boolean isBus = (M"See full answer

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

    "Make current as root. 2 while current is not null, if p and q are less than current, go left. If p and q are greater than current, go right. else return current. return null"

    Vaibhav D. - "Make current as root. 2 while current is not null, if p and q are less than current, go left. If p and q are greater than current, go right. else return current. return null"See full answer

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

    "Basic Approach As BST inorder traversal will result in a sequence of increasing order. Store that order in a vector and get the k-1 index to get the Kth smallest element, similarly access the N-K+1 th element will be the Kth largest element Time Complexity: O(n) Space Complexity O(n) Space Optimized Approach For Kth smallest , start inorder traversal, and keep a counter, decrement the counter when you access the node element. When the counter turns 0 that elementwill be the Kth smal"

    Saurabh S. - "Basic Approach As BST inorder traversal will result in a sequence of increasing order. Store that order in a vector and get the k-1 index to get the Kth smallest element, similarly access the N-K+1 th element will be the Kth largest element Time Complexity: O(n) Space Complexity O(n) Space Optimized Approach For Kth smallest , start inorder traversal, and keep a counter, decrement the counter when you access the node element. When the counter turns 0 that elementwill be the Kth smal"See full answer

    Data Structures & Algorithms
    Coding
    +1 more
  • Apple logoAsked at Apple 
    Software Engineer
    Data Structures & Algorithms
    +1 more
  • "int a_array[10] = {3,6,4,7,2,1,9}; int index = 0; int index2 = 0; for ( index = 0; index < sizeof(a_array); index++ ) { int tmpindex = index + 1; if ( tmpindex <= sizeof(a_array) ) { for ( index2 = tmpindex; index2 < sizeof(a_array); index2++ ) { if ( aarray[index] <= aarray[index2] ) { print( "%d is the NGE of %d" array[index2], array[index]); break; "

    Mark S. - "int a_array[10] = {3,6,4,7,2,1,9}; int index = 0; int index2 = 0; for ( index = 0; index < sizeof(a_array); index++ ) { int tmpindex = index + 1; if ( tmpindex <= sizeof(a_array) ) { for ( index2 = tmpindex; index2 < sizeof(a_array); index2++ ) { if ( aarray[index] <= aarray[index2] ) { print( "%d is the NGE of %d" array[index2], array[index]); break; "See full answer

    Software Engineer
    Data Structures & Algorithms
    +1 more
Showing 181-200 of 241