No results found for the specified position. 11 Java Beginner Level Implementation Interview Questions

MockQuestions

Java Beginner Level Implementation Mock Interview

To help you prepare for your Java Beginner Level Implementation interview, here are 11 interview questions and answer examples.

Get More Information About Our Java Beginner Level Implementation Interview Questions

Question 1 of 11

Validate a square.

This interview question shows a developer's knowledge of Java functions.

You are given four points in 2-D space. Return true if they form a square, false otherwise. The points are integer coordinates and are given in any order.

Valid square- A quadrilateral with all sides equal and both diagonal equal. Also, each interior angle is equal to 90°.

/*Example*/

Input- [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1]
Expected output- true

Input- [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,12]
Expected output- false

Solution:

For four points, we can find distances between them. There are a total of 6 distances possible. To be a valid square, 4 of them should be equal (the sides) and 2 should be equal (diagonals), and satisfies the following condition (Pythagoras theorem)-
diagonal2 = 2 * side2

Instead of storing actual sides or diagonal, we are storing their square to avoid precision loss due to imperfect squares.

import java.util.*;
 
class Solution {
    public boolean validSquare(int[] p1, int[] p2, int[] p3, int[] p4){
        ArrayList<Double> distances = new ArrayList<Double>();
        ArrayList<int[]> points = new ArrayList<int[]>();
        points.add(p1);
        points.add(p2);
        points.add(p3);
        points.add(p4);
        
        for (int i = 0; i < 4; i++) {
            int[] point1 = points.get(i);
            for (int j = i + 1; j < 4; j++) {
                int[] point2 = points.get(j);
                distances.add(getDistanceSquared(point1, point2));
            }
        }
        
        ArrayList<Double> d1 = new ArrayList<Double>();
        ArrayList<Double> d2 = new ArrayList<Double>();
        for (int i = 0; i < distances.size(); i++) {
            Double firstDistance = distances.get(0);
            Double ithDistance = distances.get(i);

            if (Double.compare(firstDistance, ithDistance) == 0) {
                d1.add(ithDistance);
            } else {
                d2.add(ithDistance);
            }
        }

        //Remove value which doesn’t match with the first value. 
        d2.removeIf(n -> (Double.compare(n, d2.get(0)) != 0));
        
        return areValidSides(d1, d2) || areValidSides(d2, d1);
    }
 
    private boolean areValidSides(ArrayList<Double> diagonals,
 ArrayList<Double> sides) {
        if (diagonals.size() != 2 || sides.size() != 4) {
            return false;
        }
        return (sides.get(0) * 2 == diagonals.get(0));
    }
 
    private double getDistanceSquared(int[] p1, int[] p2) {
        double diffXSquared = Math.pow(p1[0] - p2[0], 2);
        double diffYSquared = Math.pow(p1[1] - p2[1], 2);
        return diffXSquared + diffYSquared;
    }
}

Time complexity- O(1) // only four points
Space complexity- O(1)

Written by on May 21st, 2021

Next Question

11 Java Beginner Level Implementation Interview Questions & Answers

Below is a list of our Java Beginner Level Implementation interview questions. Click on any interview question to view our answer advice and answer examples. You may view 5 answer examples before our paywall loads. Afterwards, you'll be asked to upgrade to view the rest of our answers.

  • 1. Validate a square.

  • 2. Format a string by grouping characters found in the string into similar lengths.

  • 3. How do you find two integers in the array such that their sum is 'target'?

  • 4. How do you determine if a string is valid or not?

  • 5. How can you find the third largest number in an array?

  • 6. Find if the array contains a unique number of occurrences for each value in the array or not.

  • 7. Determine if str1 is a subsequence of str2.

  • 8. Find the largest two numbers in an array without sorting them.

  • 9. Find unique vanishing numbers.

  • 10. Create a running sum of a 1-D array.

  • 11. Find if the robot returns to its home after given instructions or not.