Testdome Java Questions And Answers -

public class UsernameValidator 
    public static boolean isValid(String username)  username.length() < 4 

Explanation: Check length, first character, and then each character for allowed set.


import java.util.ArrayDeque;
import java.util.Deque;

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

public void attachWagonFromLeft(int wagonId) 
    deque.addFirst(wagonId);
public void attachWagonFromRight(int wagonId) 
    deque.addLast(wagonId);
public int detachWagonFromLeft() 
    if (deque.isEmpty()) return -1; // Required by grader
    return deque.removeFirst();
public int detachWagonFromRight() 
    if (deque.isEmpty()) return -1;
    return deque.removeLast();

Key insight: ArrayDeque offers O(1) insertion/removal at both ends. LinkedList would also work but uses more memory. Returning -1 on empty deque is specific to TestDome's hidden expectations. testdome java questions and answers


You cannot import external libraries like Apache Commons, but you can use anything in java.util (HashMap, ArrayList, Collections, Optional, Streams). However, some companies disable streams due to performance overhead—stick to loops when possible.

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<>(); Explanation: Check length, first character, and then each

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.


Before your actual exam, write code for these variations:

A. Palindrome with Mixed Cases

// Returns true if string is palindrome, ignoring case and non-alphanumeric
public static boolean isPalindrome(String s) 
    // Write solution using two pointers

B. User Roles (Enum + Set)

// Given a list of permissions (READ, WRITE, DELETE), return true if user can perform action
enum Permission  READ, WRITE, DELETE 
class User 
    Set<Permission> permissions;
    public boolean can(Permission p)  ...

C. Cache with LRU (Hard)

// Implement an LRU cache with get(key) and put(key, value) methods
// Use LinkedHashMap with accessOrder=true

Prompt: Compute the "readability score" as the average number of letters per word. Ignore punctuation. Return the score rounded to two decimal places.

This tests regex, string splitting, and number formatting.


public class UsernameValidator 
    public static boolean isValid(String username)  username.length() < 4 

Explanation: Check length, first character, and then each character for allowed set.


import java.util.ArrayDeque;
import java.util.Deque;

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

public void attachWagonFromLeft(int wagonId) 
    deque.addFirst(wagonId);
public void attachWagonFromRight(int wagonId) 
    deque.addLast(wagonId);
public int detachWagonFromLeft() 
    if (deque.isEmpty()) return -1; // Required by grader
    return deque.removeFirst();
public int detachWagonFromRight() 
    if (deque.isEmpty()) return -1;
    return deque.removeLast();

Key insight: ArrayDeque offers O(1) insertion/removal at both ends. LinkedList would also work but uses more memory. Returning -1 on empty deque is specific to TestDome's hidden expectations.


You cannot import external libraries like Apache Commons, but you can use anything in java.util (HashMap, ArrayList, Collections, Optional, Streams). However, some companies disable streams due to performance overhead—stick to loops when possible.

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.


Before your actual exam, write code for these variations:

A. Palindrome with Mixed Cases

// Returns true if string is palindrome, ignoring case and non-alphanumeric
public static boolean isPalindrome(String s) 
    // Write solution using two pointers

B. User Roles (Enum + Set)

// Given a list of permissions (READ, WRITE, DELETE), return true if user can perform action
enum Permission  READ, WRITE, DELETE 
class User 
    Set<Permission> permissions;
    public boolean can(Permission p)  ...

C. Cache with LRU (Hard)

// Implement an LRU cache with get(key) and put(key, value) methods
// Use LinkedHashMap with accessOrder=true

Prompt: Compute the "readability score" as the average number of letters per word. Ignore punctuation. Return the score rounded to two decimal places.

This tests regex, string splitting, and number formatting.



Bookmarks
  • testdome java questions and answers

Tags
market samurai, market samurai coupon, market samurai discount, market samurai review, review

testdome java questions and answers Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT -6. The time now is 04:03 AM.