Testdome Java Questions And Answers -

A train has wagons. Implement attachWagonFromLeft, attachWagonFromRight, and detachWagonFromLeft, detachWagonFromRight efficiently.

Alex’s thoughts:

His code:

import java.util.*;

public class TrainComposition private Deque<Integer> wagons = new ArrayDeque<>();

public void attachWagonFromLeft(int wagonId) 
    wagons.addFirst(wagonId);
public void attachWagonFromRight(int wagonId) 
    wagons.addLast(wagonId);
public int detachWagonFromLeft() 
    return wagons.pollFirst();
public int detachWagonFromRight() 
    return wagons.pollLast();

Efficient O(1) operations, passes all edge cases.


| Mistake | Why It Fails | |---------|---------------| | Modifying input parameters | TestDome expects immutability unless required | | Ignoring null | Hidden test passes null→ your code throws NPE | | Using == for string comparison | Works only for string literals | | Hardcoding array sizes | Fails when input changes | | Not closing resources | Causes memory leaks in hidden tests | | Overly complex O(n²) solutions | Timeout on large data hidden tests |


public class Palindrome 
    public static boolean isPalindrome(String s) 
        int left = 0, right = s.length() - 1;
    while (left < right) 
        while (left < right && !Character.isLetterOrDigit(s.charAt(left))) 
            left++;
while (left < right && !Character.isLetterOrDigit(s.charAt(right))) 
            right--;
if (Character.toLowerCase(s.charAt(left)) != Character.toLowerCase(s.charAt(right))) 
            return false;
left++;
        right--;
return true;

Explanation: Two-pointer technique skipping non-alphanumeric characters. testdome java questions and answers


Task: Merge two sorted integer arrays into one sorted array. Do not use Arrays.sort().

public class Merger 
    public static int[] mergeSorted(int[] a, int[] b) 
        int[] result = new int[a.length + b.length];
        int i = 0, j = 0, k = 0;
    while (i < a.length && j < b.length) 
        result[k++] = (a[i] <= b[j]) ? a[i++] : b[j++];
while (i < a.length) result[k++] = a[i++];
    while (j < b.length) result[k++] = b[j++];
return result;


TestDome offers 4 free Java sample questions. Solve them in a simulated environment before your real screening. Study the "Top solutions" voted by the community.


TestDome scores range 0–100% .

Most companies set a passing threshold at 75% . If you score below, they often do not proceed to the next interview round.


Task:
Implement a method that checks if a username is valid. A valid username:

Task:
Given a BST and two nodes, find their lowest common ancestor (LCA).

Definition: LCA of two nodes p and q is the deepest node that has both as descendants.