Java Interview Questions and Sample Solutions (Sorting Units, Coding Challenges, and Common Topics)

This article presents a Java interview preparation guide featuring a unit‑conversion sorting problem with complete code, two additional coding challenges, and a collection of typical interview questions on design patterns, GC troubleshooting, MySQL optimization, and Zookeeper election, plus links to extensive interview question archives.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Java Interview Questions and Sample Solutions (Sorting Units, Coding Challenges, and Common Topics)

1. Personality Test

Select positive options; note that the personality test may also affect candidate selection.

2. Programming Test

Problem: Given an array with size units such as 20M, 1T, 300G (where 1T=1000G, 1G=1000M), output the values sorted from smallest to largest.

Example Input: 20M<br/>1T<br/>300G Example Output: 20M<br/>300G<br/>1T Solution (Java):

package Huawei;<br/><br/>import java.util.Scanner;<br/><br/>/**<br/> * Created by xuzhenyu on 2020/1/5.<br/> */<br/>public class Test {<br/>    public static void main(String[] args) {<br/>        Scanner scanner = new Scanner(System.in);<br/>        int n = scanner.nextInt();<br/>        String[] strings = new String[n];<br/>        for (int i = 0; i < n; i++) {<br/>            strings[i] = scanner.next();<br/>        }<br/>        String[] resultStrs = sort(strings);<br/>        for (int i = 0; i < resultStrs.length; i++) {<br/>            System.out.println(resultStrs[i]);<br/>        }<br/>    }<br/>    private static String[] sort(String[] strs) {<br/>        for (int i = 0; i < strs.length - 1; i++) {<br/>            for (int j = 0; j < strs.length - i - 1; j++) {<br/>                // M G T<br/>                if (compare(strs[j], strs[j + 1])) {<br/>                    String tem = strs[j];<br/>                    strs[j] = strs[j + 1];<br/>                    strs[j + 1] = tem;<br/>                }<br/>            }<br/>        }<br/>        return strs;<br/>    }<br/>    private static boolean compare(String str1, String str2) {<br/>        int str1M = turnString(str1);<br/>        int str2M = turnString(str2);<br/>        return str1M > str2M;<br/>    }<br/>    private static int turnString(String str) {<br/>        if ("M".equals(String.valueOf(str.charAt(str.length() - 1)))) {<br/>            return Integer.parseInt(str.substring(0, str.length() - 1));<br/>        } else if ("G".equals(String.valueOf(str.charAt(str.length() - 1)))) {<br/>            return Integer.parseInt(str.substring(0, str.length() - 1)) * 1000;<br/>        } else if ("T".equals(String.valueOf(str.charAt(str.length() - 1)))) {<br/>            return Integer.parseInt(str.substring(0, str.length() - 1)) * 1000000;<br/>        }<br/>        return 0;<br/>    }<br/>}<br/>

3. Additional Interview Questions

3.1 Two Coding Problems

(1) Given an array where only one element appears once and all others appear twice, output the unique element. (Reference to previous interview collections.)

Example Input: {2,2,1,1,4,4,7}

Output: 7

(2) See the attached image for the second coding problem.

3.2 Interview Topics

(1) Describe common factory design patterns.

(2) Steps to locate a Full GC performance issue.

(3) MySQL database optimization techniques.

(4) Zookeeper leader election mechanism.

These constitute the core interview segment.

Additional Resources

Links to extensive interview question archives (e.g., 001–180 issues, HashMap interview Q&A, Spring Cloud questions, SQL optimization, Redis topics, etc.) are provided for further study.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendDesign PatternsJavainterviewcoding test
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.