20 questions · untimed

Java interview: Coding round (Java)

Pick an option and check it. The explanation shows after each answer. Take as long as you like; nothing is timed and nothing is sent.

20 questions

Question 1 · easy

What does this Java 21 program print?

import java.util.Arrays;
public class Main {
    static void reverse(int[] a, int lo, int hi) {
        while (lo < hi) {
            int t = a[lo]; a[lo] = a[hi]; a[hi] = t;
            lo++; hi--;
        }
    }
    public static void main(String[] args) {
        int[] a = {1, 2, 3, 4, 5, 6};
        reverse(a, 1, 4);
        System.out.println(Arrays.toString(a));
    }
}

Question 2 · easy

What does this Java 21 program print?

public class Main {
    static boolean isPalindrome(String s) {
        int i = 0, j = s.length() - 1;
        while (i < j) {
            if (s.charAt(i) != s.charAt(j)) return false;
            i++; j--;
        }
        return true;
    }
    public static void main(String[] args) {
        System.out.println(isPalindrome("racecar") + " " + isPalindrome("Madam")
            + " " + isPalindrome("abba"));
    }
}

Question 3 · medium

What does this Java 21 program print?

public class Main {
    static boolean isAnagram(String a, String b) {
        if (a.length() != b.length()) return false;
        int[] count = new int[26];
        for (int i = 0; i < a.length(); i++) {
            count[a.charAt(i) - 'a']++;
            count[b.charAt(i) - 'a']--;
        }
        for (int c : count) if (c != 0) return false;
        return true;
    }
    public static void main(String[] args) {
        System.out.println(isAnagram("listen", "silent") + " " + isAnagram("aab", "abb"));
    }
}

Question 4 · medium

This method should reverse a string in place in a char[]. reverse("abc") returns cba, but reverse("abcd") returns dbca. Which change fixes it?

static String reverse(String s) {
    char[] c = s.toCharArray();
    int n = c.length;
    for (int i = 0; i <= n / 2; i++) {
        char t = c[i];
        c[i] = c[n - 1 - i];
        c[n - 1 - i] = t;
    }
    return new String(c);
}

Question 5 · easy

A candidate builds a comma-separated string of n numbers with result += nums[i] + ","; inside a loop. What should they say about this when the interviewer asks about performance?

Question 6 · medium

What does this Java 21 program print?

import java.util.*;
public class Main {
    public static void main(String[] args) {
        int[] nums = {3, 2, 4};
        int target = 6;
        Map<Integer, Integer> seen = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            Integer j = seen.get(target - nums[i]);
            if (j != null) { System.out.println(j + " " + i); return; }
            seen.put(nums[i], i);
        }
        System.out.println("none");
    }
}

Question 7 · easy

What does this Java 21 program print?

import java.util.*;
public class Main {
    public static void main(String[] args) {
        String s = "loveleetcode";
        Map<Character, Integer> freq = new HashMap<>();
        for (char c : s.toCharArray()) freq.merge(c, 1, Integer::sum);
        int answer = -1;
        for (int i = 0; i < s.length(); i++) {
            if (freq.get(s.charAt(i)) == 1) { answer = i; break; }
        }
        System.out.println(answer);
    }
}

Question 8 · medium

What does this Java 21 program print?

import java.util.*;
public class Main {
    public static void main(String[] args) {
        String[] words = {"eat", "tea", "tan", "ate", "nat", "bat"};
        Map<String, List<String>> groups = new HashMap<>();
        for (String w : words) {
            char[] c = w.toCharArray();
            Arrays.sort(c);
            groups.computeIfAbsent(new String(c), k -> new ArrayList<>()).add(w);
        }
        System.out.println(groups.size() + " " + groups.get("aet"));
    }
}

Question 9 · easy

This method should count each character, but counts("aba") throws a NullPointerException. Which replacement for the line inside the loop makes it count a as 2 and b as 1?

static Map<Character, Integer> counts(String s) {
    Map<Character, Integer> map = new HashMap<>();
    for (char c : s.toCharArray()) {
        map.put(c, map.get(c) + 1);
    }
    return map;
}

Question 10 · hard

Given an unsorted int[] of n values, return the length of the longest run of consecutive integers (for {100, 4, 200, 1, 3, 2} the answer is 4, from 1, 2, 3, 4). The interviewer asks for O(n) expected time. Which approach meets it?

Question 11 · easy

What does this Java 21 program print?

import java.util.*;
public class Main {
    static boolean valid(String s) {
        Deque<Character> stack = new ArrayDeque<>();
        for (char c : s.toCharArray()) {
            int k = "([{".indexOf(c);
            if (k >= 0) stack.push(")]}".charAt(k));
            else if (stack.isEmpty() || stack.pop() != c) return false;
        }
        return stack.isEmpty();
    }
    public static void main(String[] args) {
        System.out.println(valid("{[()]}") + " " + valid("([)]") + " " + valid("(("));
    }
}

Question 12 · medium

A queue is built from two stacks (ArrayDeques used with push and pop). offer(x) pushes onto in. poll() pops from out, and only when out is empty does it first pop every element of in and push it onto out. What is the time complexity of poll()?

Question 13 · hard

What does this Java 21 program print?

import java.util.*;
public class Main {
    public static void main(String[] args) {
        int[] a = {2, 7, 3, 5, 4, 6, 8};
        int[] next = new int[a.length];
        Arrays.fill(next, -1);
        Deque<Integer> stack = new ArrayDeque<>();
        for (int i = 0; i < a.length; i++) {
            while (!stack.isEmpty() && a[stack.peek()] < a[i]) next[stack.pop()] = a[i];
            stack.push(i);
        }
        System.out.println(Arrays.toString(next));
    }
}

Question 14 · medium

This BFS should list each node once in breadth-first order. On the graph with edges 0 -> 1, 0 -> 2, 1 -> 3 and 2 -> 3 it returns [0, 1, 2, 3, 3]. Which fix is right?

static List<Integer> bfs(List<List<Integer>> adj, int start) {
    List<Integer> order = new ArrayList<>();
    Deque<Integer> queue = new ArrayDeque<>();
    boolean[] visited = new boolean[adj.size()];
    queue.offer(start);
    while (!queue.isEmpty()) {
        int u = queue.poll();
        visited[u] = true;
        order.add(u);
        for (int v : adj.get(u)) if (!visited[v]) queue.offer(v);
    }
    return order;
}

Question 15 · medium

Design a stack of ints that supports push, pop, top and getMin, each in O(1) time. Which design works?

Question 16 · easy

What is the time complexity of this fragment in terms of n?

int count = 0;
for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
        count++;
    }
}

Question 17 · medium

An interviewer asks: what is the time complexity of HashMap.get(key) in Java 21?

Question 18 · medium

jobs is an ArrayList with n = 100,000 items, and this method is far slower than expected. What is the cause and the right fix?

static long sumAll(List<Integer> jobs) {
    long total = 0;
    while (!jobs.isEmpty()) {
        total += jobs.remove(0);
    }
    return total;
}

Question 19 · easy

What does this Java 21 program print?

public class Main {
    public static void main(String[] args) {
        int n = 100, outer = 0, inner = 0;
        for (int i = 1; i < n; i *= 2) {
            outer++;
            for (int j = 0; j < n; j++) inner++;
        }
        System.out.println(outer + " " + inner);
    }
}

Question 20 · hard

You must report whether an int[] of n values contains a duplicate, in O(n log n) time, without allocating any extra collection or array (modifying the input is allowed). Which approach meets both limits?

Next: Spring Boot round