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?

### Analysis

• If duplicates are not allowed, sort the array (Arrays.sort(…)) and get the second last element, which executes in O(nlogn)
• If duplicates are allowed, loop through each element have two variables to store highest & second highest values. which executes in O(n)

Tags:

## 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)

## 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. Check for pre-conditions and post conditions. For, example, if you are writing a function to “apply given interest rate to given balance”.

Pre-condition: Balance is a positive number and greater than zero. Interest rate is not a percentage and can’t be 1.

Post-condition: new balance …

Tags:

## 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…

## 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

## 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.

## 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”. For…

## 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

Tags:

## 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. Other perfect numbers…

## 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”? A1. This is a very basic coding question,…

## 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. So, “rand.nextInt(9)” will return 9 numbers between 0 and 8 inclusive. To start with 1, add…

## 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. For example, if you have…

## 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?
A1.

### Iteration:

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”.

## 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.

1) Convert the input to lowercase to make the evaluation case insensitive.

2) Store each character in a …

Tags:

## 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?

#### 50+ Java coding practice problems Links:

Q1. Can you write code to check if a given number is prime?
A1. A prime …

Tags:

## 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….

## Palindrome

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”. The longest palindromic substring of “bananas” is…

## 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

## 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…

## 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.

ALGORITHM to use

1. Create a BST (Binary Search Tree) and while creating BST maintain the frequency of …

1 2

Top