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 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. How does a hash map handle collisions?

    A collision happens when two keys hash to the same bucket. Chaining stores colliding entries in a list inside the bucket; open addressing probes for the next free slot. Both keep lookups near O(1) on average if the table stays sparse.

    BeginnerQuick answer1 min read
  14. What happens if a recursive function has no base case?

    It keeps calling itself, each call adds a frame to the call stack, and eventually the stack runs out of space. JavaScript throws a RangeError: Maximum call stack size exceeded.

    BeginnerQuick answer1 min read
  15. What is the difference between a stack and a queue, with real examples?

    A stack is last in, first out (LIFO): you add and remove at the same end. A queue is first in, first out (FIFO): you add at the back and remove from the front.

    BeginnerQuick answer1 min read
  16. What is the difference between an array and a linked list, and when would you choose each?

    An array stores items contiguously, so indexing is O(1) but inserting in the middle is O(n). A linked list stores nodes connected by pointers, so insertion at a known node is O(1) but reaching the kth item is O(n).

    BeginnerQuick answer1 min read
  17. When should you use a Map instead of a plain object in JavaScript?

    Use a Map when keys are not just strings, when you add and remove entries often, or when you need a reliable size and insertion order. Use a plain object for fixed, string-keyed records.

    BeginnerQuick answer1 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