Blog Archives
1 2

05: Finding the 2nd highest number in an array in Java

Requirements gathering

  1. Does the array allow duplicates?
  2. If duplicates are allowed, then do you need to report duplicates? For example, in {2,4, 6, 3, 6, 5}, is 6 or 5 the second highest?


  • If duplicates are not allowed, sort the array (Arrays.sort(…))
Read more ...

08: Write code to add, subtract, multiply, and divide given numbers?

A trivial coding example (i.e. a Calculator) tackled using the following programming paradigms in Java not only to perform well in coding interviews, but also to learn these programming paradigms.

Approach 1: Procedural Programming
Approaches 2 – 4: Object Oriented Programming
Approach 5: Functional Programming (Java 8)

Approach 1: Procedural

Read more ...

20 Java coding interview questions – tips & considerations

Java coding interview questions are very common in job interviews. Good coding skills are essentials for passing the peer code reviews with flying colours. Here are 17 coding tips with Java examples.

Tip #1: If you are asked to write a function or code, make sure that your code fails fast.… Read more ...


Fibonacci number

Q1. Can you write a function to determine the nth Fibonacci number? The Fibonacci numbers under 2000 are : 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597. Where the zeroth number being 0, first number being 1, second number being…

Read more ...

Fibonacci number with caching and Java 8 FP

Complimenting Fibonacci number coding – iterative and recursive approach, we can improve performance by caching. If you run this

Read more ...

Find 2 numbers from an array that add up to the target

Q1. Given an array of integers, find two numbers such that they add up to a specific target number? For example, Given numbers: {2, 3, 8, 7, 5} Target number: 9 Result: 2 and 7 A1. Solution 1: Store processed numbers in a set.… Read more ...

Find the first non repeated character in a given string input

Firstly, understand the problem, and write down any considerations & assumptions. Next write pseudocode if that helps. Considerations 1) Pre-codnition check for null or empty input. 2) Loop through the input string, and store each “character” as a key in a map with the value being the “character count”.… Read more ...

Finding the missing numbers Java example

Q. Can you write code to identify missing numbers in a given array of numbers?

Solution 1: Assuming that the given numbers are in order

Read more ...

Finding the perfect number

Q. Can you write code to output the perfect number between a given range? Definition: A perfect number is a positive integer that is equal to the sum of its proper divisors. The smallest perfect number is 6, which is the sum of 1, 2, and 3.… Read more ...

Fizz Buzz

Q1. Write a program that prints numbers from 1 to 30, but for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”?… Read more ...

Generating random numbers in Java

Q1. Can you write Java code to generate random numbers between a given range? A1. E.g. 0 and 9 or 5 to 35, and so on.

The “nextInt()” method works from “0” onwards.… Read more ...

Huffman coding in Java

This is a very decent coding exercise to sharpen your coding skills. Nicely explained with diagrams and complete code. Q1. What is Huffman coding? A1. Huffman coding is a compression technique used to reduce the number of bits needed to send or store a message.… Read more ...

Iteration Vs Recursion in Java

Q1. Can you write a sample code that will count the number of “A”s in a given text “AAA rating“? Show both iterative and recursive approaches?


A for loop to traverse through each character of the String “AAA rating”, and a if condition to check for “A” & increment “count” by 1 if the character is “A”.… Read more ...

Java 8 String streams and finding the first non repeated character with functional programming

Q1.Find the first non repeated character in a given string input using Java 8 or later?
A1.Extends Find the first non repeated character in a given string input with Java 8 functional programming.

For example, an input string of “stress” would have

Key -Value
s -3
t -1
r -1
e -1

and “t” would be the output as it is the first non-repeated character as it has a count of 1.… Read more ...


Java coding practice problems – prime and powerful

There is no other way to pass the Java coding tests other than practicing. I have failed Java coding tests due to lack of practice. How often do you get work with tree and graph data structures? Ho often do you work with algorithms?… Read more ...


Java coding question on recursion and generics

Q. Can you write Java code to compute a collection of numbers supplied to it? The computation could be addition, subtraction, etc. Use recursion to compute the numbers. Here are some requirements to take into considerations. 1. It should be flexible enough to convert from Recursion to iteration if required….… Read more ...


Q1. Can you write a function to determine if a given string input is a palindrome? A palindrome is a word or sentence that reads the same forward as it does backward. For example, the terms “racecar”, “dad”, “madam” and the name “Hannah”.… Read more ...

Reverse a given String

Q1. Can you write a method that reverses a given String? A1. Can be done a number of different ways. Best Practice: Using the Proven Java API

Read more ...

Reverse Polish Notation (RPN)

Q1. Can you explain Reverse Polish Notation (RPN)? A1. You have already heard about the following from your elementary schooling: “Please Excuse My Dear Aunt Sally” (meaning Parentheses, Exponentiation (roots and powers), Multiplication, Division, Addition, and Subtraction BODMAS, tells us to attend to Brackets, Of, Division, Multiplication, Addition, and Subtraction…

Read more ...

Sorting a list of numbers by frequencies in Java using a BST tree

This extends Sorting a list of numbers by frequencies in Java using a map to use a BST instead of a Map.

PROBLEM to solve: Sort a list of numbers by frequency of their occurrences. For example

INPUT: [ 5, 3, 7, 7, 7, 5, 4, 8 ]

OUTPUT: [7, 7, 7, 5, 5, 3, 4, 8]

Pre-requisite: Assumes that you understand the basics concepts like recursion, tree traversal algorithms and Basic Java Tree structure interview Q&A with coding.… Read more ...

1 2
Don't be overwhelmed by the number of Q&As & tech stacks as nobody knows everything, and often key Q&As at the right moment makes a difference.

500+ Java Interview FAQs

Java & Big Data Tutorials