Interview Q&A / Data Structures & Algorithms

INTERVIEW Q&A · 55 QUESTIONS

Data Structures & Algorithms interview questions

Arrays, linked lists, hashing, trees and patterns, with line-by-line code and complexity.

  1. How do you check if a string of brackets is valid (balanced parentheses)?

    Scan the string with a stack. Push every opening bracket. For every closing bracket, the stack must be non-empty and its top must be the matching opening bracket, which you then pop. The string is valid only if the stack is empty at the end. O(n) time, O(n) space.

    BeginnerIn-depthCoding3 min read
  2. How do you implement binary search, and what are the common bugs?

    On a sorted array, keep two pointers low and high, check the middle element, and throw away the half that cannot contain the target. Repeat until found or low passes high. It runs in O(log n). The classic bugs are computing mid as (low + high) / 2, which can overflow in Java or C++, and off-by-one errors in the loop condition and pointer updates.

    BeginnerIn-depthCoding4 min read
  3. How do you merge two sorted arrays into one sorted array?

    Use two pointers, one at the start of each array. Compare the two current elements, copy the smaller one to the result and move that pointer forward. When one array runs out, copy the rest of the other. This is O(n + m) time, much better than joining and sorting again.

    BeginnerIn-depthCoding3 min read
  4. How do you reverse a singly linked list?

    Walk through the list once with three pointers (previous, current and next) and turn each arrow around so it points backwards. The last node you visit becomes the new head. O(n) time, O(1) extra space.

    BeginnerIn-depthCoding2 min read
  5. Two Sum - find two numbers that add up to a target

    Walk through the array once and store each number's index in a hash map. For each number, check if target minus that number is already in the map. This takes O(n) time.

    BeginnerIn-depthCoding2 min read
  6. How do you check if a string is a palindrome, ignoring spaces, punctuation and case?

    Use two pointers from both ends. Skip characters that are not letters or digits, compare the rest in lowercase, and move inward. If any pair differs, it is not a palindrome.

    BeginnerQuick answerCoding1 min read
  7. How do you check if one string is a rotation of another?

    They must have the same length, and the second string must appear inside the first string written twice. For example, "waterbottle" doubled contains "erbottlewat".

    BeginnerQuick answerCoding1 min read
  8. How do you check whether two strings are anagrams of each other?

    Two strings are anagrams if they have the same letters with the same counts. Count the characters of the first string, subtract using the second, and check that every count ends at zero.

    BeginnerQuick answerCoding1 min read
  9. How do you evaluate a postfix (reverse Polish) expression?

    Scan tokens left to right. Push numbers onto a stack. When you see an operator, pop two numbers, apply the operator (the first popped is the right operand), and push the result. The final stack value is the answer.

    BeginnerQuick answerCoding1 min read
  10. How do you find the first non-repeating character in a string?

    Count how often each character appears in one pass, then scan again and return the first character whose count is 1.

    BeginnerQuick answerCoding1 min read
  11. How do you find the longest common prefix among an array of strings?

    Take the first string as the candidate prefix and shorten it until every other string starts with it. Return what is left, which may be an empty string.

    BeginnerQuick answerCoding1 min read
  12. How do you find the maximum profit from buying and selling a stock once?

    Scan once while tracking the lowest price so far. At each day, the best profit if you sell today is price minus that lowest price. Keep the maximum of those.

    BeginnerQuick answerCoding1 min read
  13. How do you find the middle node of a linked list in one pass?

    Use two pointers: slow moves one step, fast moves two. When fast reaches the end, slow is at the middle. For an even length it lands on the second of the two middle nodes.

    BeginnerQuick answerCoding1 min read
  14. How do you implement merge sort in JavaScript?

    Recursively split the array into two halves until single elements remain, then merge the sorted halves by repeatedly taking the smaller front element. It runs in O(n log n) time with O(n) extra space.

    BeginnerQuick answerCoding1 min read
  15. How do you merge two sorted linked lists into one sorted list?

    Use a dummy head and a tail pointer. Repeatedly attach the smaller of the two current nodes to the tail and advance that list. When one list ends, attach the rest of the other.

    BeginnerQuick answerCoding1 min read
  16. How do you remove duplicates from a sorted array in place and return the new length?

    Keep a write index. Because the array is sorted, a value is new only if it differs from the last value written. Copy it forward and advance the write index; the final write index is the new length.

    BeginnerQuick answerCoding1 min read
  17. How do you reverse the order of words in a sentence, ignoring extra spaces?

    Trim the string, split on one or more whitespace characters, reverse the array of words, and join with a single space.

    BeginnerQuick answerCoding1 min read
  18. Why does [10, 9, 1].sort() return [1, 10, 9] in JavaScript?

    By default sort converts elements to strings and compares them in dictionary order, so "10" comes before "9". Pass a comparator such as (a, b) => a - b to sort numbers numerically.

    BeginnerQuick answerCoding1 min read
  19. Write a function that moves all zeroes to the end of an array while keeping the order of the other elements.

    Use a write pointer: copy every non-zero value forward to the write position, then fill the remaining slots with zeroes. One pass, no extra array.

    BeginnerQuick answerCoding1 min read