Blog Archives
1 2 3 4 5 6

00: Tree from a list & flattening it back to a list in Java

Hierarchical data with parent & child relationships are very common, and Java collection does not have a Tree data structure, hence it is a popular interview question. Further questions include tree data structure processing using recursiion & iteration. A Tree data structure can be represented as shown below.

Step 1: A simple Node.java class that encapsulates its value, parent node id & if any child nodes. It is important that “parent” is not included in the toString(..) method as it leads to Stack Overflow error due to recursive calls.

Tags:

01: Can you design classes & interfaces using OO?

Q1. How would you go about designing a “farm animals” application where animals like cow, pig, horse, etc move from a barn to pasture, a stable to paddock, etc? The solution should also cater for extension into other types of animals like circus animals, wild animals, etc in the future….



01: String parsing & date formatting – complete the given code

Q. Complete the method “changeDateFormat(String paragraph)” which takes a string input containing dates in mm/dd/yyyy format and converts them to “dd/mm/yyyy” format. For example, “My birthday is on 04/25/1980” becomes “My birthday is on 25/04/1980



01: Tree flattening example in Java with Generics

Q. Traverse the tree in depth and return a list of all passed nodes. It should handle different data types as Double, String, etc. The interface for the tree node is

A. Tree storing numbers: Tree storing text: #1. Define the …



01: What is wrong with following Java code snippets? Auto-unboxing, switch, short circuit & exception

Auto-unboxing, switch, short circuit logical operators & exception handling are core Java basics that sometimes lead to obscure errors if not understood well & often tested in coding tests.

Q1. What is wrong with this code?



02: Scenarios based Java OO concepts & GoF design patterns – naive Template Method pattern

Scenarios based Java OO concepts & GoF design patterns


02: Tree structure – calculate average recursively & iteratively in Java

Q. Complete the following “TreeProcessingImpl” class ….. so that the unit tests shown below pass? Skeleton Code



02: Working with Java Collections – Complete the given code

Q. Complete the method “canAttendAll(Collection seminars)” which takes a collection of seminar objects with start and end timestamps, and the method should determine if there is an overlap in seminars events. In other words, if you can attend all the seminars.



03: Scenarios based Java OO concepts & GoF design patterns – Strategy Pattern

The following post improves on the design flaws listed in the previous post entitled 02: Scenarios based Java OO design & GoF design patterns – naive Template Method pattern. Scenario: A company named XYZ Retail is in the business of selling Books, CDs and Cosmetics. Books are sales tax exempt…



03: Tree navigation – is a BST Tree?

Q. Complete the method “isValidBST(Node root)” which takes a “Tree node” as an input to evaluate if the input is a valid BST (i.e. Binary Search Tree)?



03: Tree traversal algorithms in Java

Q1. What are the different binary tree traversal mechanisms? A1. Traversing a tree means visiting all the nodes of a tree in order. Many different binary tree algorithms involve traversals. For example, if you wish to count the number of employees in an organizational chart you must visit each node….



03: Understanding Big O notations with Java

Q1.  Have  you seen job advertisements requiring Java candidates to work in real-time or high volume transaction processing systems? A1.  If you are applying for such jobs, you can be quizzed on Big O notation. Here are some basics to brush up on. Big O gives you the upper bound. For example,…



03: What is wrong with this code? Data types & equals( ) Vs, hashCode( ) contract

Q6. What is wring with this code?



04: 7 Java Collection Quizzes

Q1. When is an object needs to implement the Comparable interface? a) When adding it to a HashSet. b) When adding it to a TreeSet. c) When adding it to a LinkedHashSet. d) When adding it to any Java Collection class A1 The answer is b. 1. HashSet is implemented…



04: Are Anagrams – Complete the given code

Q. Complete the “areAnagrams(String a, String b)” method so that it returns if two given input strings are anagram or not?



04: Convert Lists to Maps and Maps to Lists with Java 8 power

Why convert from a list to a map? Maps allow you to store and access data by a “KEY”. Lists only allow you to access data by either index or by looping through the whole list to find a value that you are interested in. Also, a map can be…



04: Tree – Get root to leaf max sum in Java example

Q. Write code to traverse a tree and get the largest “root-to-leaf sum”? A “root-to-leaf sum” is a sum of the node values in a root-to-leaf path.

A. Let’s have a basic implementation of the above Node interface.



04: What is wrong with this code? Java Collection & ConcurrentModificationException

Q9. What is wring with this code?



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)

Solution: duplicates are allowed

Tags:

05: Is a palindrome – complete the given code

Q. Complete the method “isPalindrome(String str) ” which takes a “string” as an input to evaluate if the input is a palindrome?



1 2 3 4 5 6

300+ Java & Big Data FAQs - Quick Prep

Java & Big Data Tutorials

Top