Interview Q&A / Java

INTERVIEW Q&A · 72 QUESTIONS

Java interview questions

Collections, the JVM, strings, exceptions and threads, the backbone of most campus placement rounds.

  1. Write a Java method that reverses the order of words in a sentence.

    Trim the text, split on one or more spaces, then join the pieces from last to first with a single space. It takes O(n) time and O(n) space for the word array and result.

    BeginnerQuick answerCoding1 min read
  2. Write a Java method to check whether a string is a palindrome, ignoring case and non-letters.

    Use two pointers from both ends, skip anything that is not a letter or digit, compare lowercase characters, and move inward. It runs in O(n) time with O(1) extra space.

    BeginnerQuick answerCoding1 min read
  3. Write a Java method to check whether two lowercase strings are anagrams.

    If the lengths differ the answer is no. Otherwise count each letter of the first string up and each letter of the second down in an int[26], and the strings are anagrams if every count ends at zero. O(n) time, O(1) space.

    BeginnerQuick answerCoding1 min read
  4. Write a Java method to remove duplicates from a list while keeping the original order.

    Put the elements into a LinkedHashSet, which drops repeats and remembers first-seen order, then copy it back into a list. It takes O(n) time and O(n) space.

    BeginnerQuick answerCoding1 min read