In this article, we would be going through the mostly asked Java Regular expression interview questions and answers. Even if you are not a great fan of interview questions, this post may offer you some interesting excercises on Regular Expressions. Java Regular Expression Interview Questions and Answers What are the classes in Java that helps to […]

# Category: Java interview questions

## Java Collection Interview Questions and Answer

Here is a list of Java Collections interview Questions. These are some of the famous questions asked in interviews. Java Collections Interview Questions What is rehashing? When is it done? In the case of a HashMap, there are two important member fields which determine when exactly the resizing of the map needs to be done. […]

## Why is String immutable in java?

This question has been asked in a number of interviews in the past and is still being asked. If you search google for this question you will find people trying to answer this question. But no one has been able to answer it properly and no one except the Java designers can really answer. We […]

## Predict the Winner -Leetcode

Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from either end of the array followed by the player 2 and then player 1 and so on. Each time a player picks a number, that number will not be available for the next player. This continues until all […]

## Leetcode – Zuma Game

Think about Zuma Game. You have a row of balls on the table, colored red(R), yellow(Y), blue(B), green(G), and white(W). You also have several balls in your hand. Each time, you may choose a ball in your hand, and insert it into the row (including the leftmost place and rightmost place). Then, if there is […]

## Number Complement – Leetcode

Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation. Note: The given integer is guaranteed to fit within the range of a 32-bit signed integer. You could assume no leading zero bit in the integer’s binary representation. Example 1:

1 2 3 |
Input: 5 Output: 2 Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2. |

Example 2:

1 2 3 |
Input: 1 Output: 0 Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0. |

[…]

## Max Consecutive Ones – Leetcode

Given a binary array, find the maximum number of consecutive 1s in this array. Example 1:

1 2 3 4 |
Input: [1,1,0,1,1,1] Output: 3 Explanation: The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3. |

Note: The input array will only contain 0 and 1. The length of input array is a positive integer and will not exceed 10,000 Solution This is an easy to solve problem. We keep on counting the ones till a […]

## Construct the Rectangle Leetcode

Given a specific rectangular web page’s area, your job by now is to design a rectangular web page, whose length L and width W satisfy the following requirements:

1 2 3 4 5 |
1. The area of the rectangular web page you designed must equal to the given target area. 2. The width W should not be larger than the length L, which means L >= W. 3. The difference between length L and width W should be as small as possible. |

You need to output the length L and the width W of the web page you designed in sequence. Example:

1 2 3 4 |
Input: 4 Output: [2, 2] Explanation: The target area is 4, and all the possible ways to construct it are [1,4], [2,2], [4,1]. But according to requirement 2, [1,4] is illegal; according to requirement 3, [4,1] is not optimal compared to [2,2]. So the length L is 2, and the width W is 2. |

Note: The given area […]

## Binary Tree Paths

Problem Given a binary tree, return all root-to-leaf paths. Define a tree with left and right nodes

1 2 3 4 5 6 7 8 9 10 11 |
package com.javagists.learn.interview.questions; // author javagists.com class TreeNode { TreeNode left, right; int val; TreeNode(int val) { this.val = val; left = right = null; } } |

Create Binary Tree and print path

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
package com.javagists.learn.interview.questions; // author javagists.com public class BinaryTree { TreeNode root; void printAllPaths(TreeNode node) { int path[] = new int[50]; printPathsRecur(node, path, 0); } void printPathsRecur(TreeNode node, int path[], int pathLen) { if (node == null) { return; } path[pathLen] = node.val; pathLen++; if (node.left == null && node.right == null) { printArray(path, pathLen); } else { printPathsRecur(node.left, path, pathLen); printPathsRecur(node.right, path, pathLen); } } void printArray(int ints[], int len) { for (int i = 0; i < len; i++) { System.out.print(ints[i] + " " ); } System.out.println(""); } public static void main(String args[]) { BinaryTree tree = new BinaryTree(); tree.root = new TreeNode(3); tree.root.left = new TreeNode(4); tree.root.right = new TreeNode(5); tree.root.left.left = new TreeNode(6); tree.root.left.right = new TreeNode(9); tree.root.right.left = new TreeNode(2); tree.printAllPaths(tree.root); } } |