0% found this document useful (0 votes)
11 views5 pages

Java Coding Challenges Overview

The document outlines three Java coding challenges: finding the longest repeating adjacent pattern in a string, determining if a valid binary tree can be formed from parent-child pairs, and converting a written mathematical expression into a numeric expression and evaluating it. Each challenge includes a problem statement, example inputs and outputs, and a Java solution. The solutions utilize data structures like HashMap and HashSet to efficiently solve the problems.

Uploaded by

Abhinay Soni
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views5 pages

Java Coding Challenges Overview

The document outlines three Java coding challenges: finding the longest repeating adjacent pattern in a string, determining if a valid binary tree can be formed from parent-child pairs, and converting a written mathematical expression into a numeric expression and evaluating it. Each challenge includes a problem statement, example inputs and outputs, and a Java solution. The solutions utilize data structures like HashMap and HashSet to efficiently solve the problems.

Uploaded by

Abhinay Soni
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Coding Challenge Documentation

1. Searching Challenge

### Problem Statement


Given a string, find the **longest repeating adjacent pattern** that appears at least **twice**.
- If a valid pattern exists, return **"yes <pattern>"**.
- Otherwise, return **"no null"**.

### Example Inputs & Outputs


| Input | Output |
|--------|--------|
| "da2kr32a2" | "yes a2" |
| "sskfssbbb9bbb" | "yes bbb" |
| "123224" | "no null" |

### Java Solution


```java
import [Link];

public class SearchingChallenge {


public static String searchingChallenge(String str) {
String longestPattern = null;

for (int len = 2; len <= [Link]() / 2; len++) {


HashMap<String, Integer> map = new HashMap<>();
for (int i = 0; i <= [Link]() - len; i++) {
String sub = [Link](i, i + len);
[Link](sub, [Link](sub, 0) + 1);
if ([Link](sub) > 1) {
if (longestPattern == null || [Link]() > [Link]()) {
longestPattern = sub;
}
}
}
}
return longestPattern != null ? "yes " + longestPattern : "no null";
}
public static void main(String[] args) {
[Link](searchingChallenge("da2kr32a2")); // yes a2
}
}
```

2. Array Challenge

### Problem Statement


Given an array of **parent-child** pairs representing a **tree structure**, determine if a **valid binary
tree** can be formed.
- A **binary tree** must have at most **two children per parent**.
- There must be exactly **one root node** (not a child of any other node).
- The tree **must not contain cycles**.

### Java Solution


```java
import [Link].*;

public class ArrayChallenge {


public static String arrayChallenge(String[] strArr) {
HashMap<Integer, List<Integer>> parentToChildren = new HashMap<>();
HashSet<Integer> allChildren = new HashSet<>();

for (String pair : strArr) {


pair = [Link]("[()]", "");
String[] parts = [Link](",");
int child = [Link](parts[0]);
int parent = [Link](parts[1]);

[Link](child);
[Link](parent, new ArrayList<>());
[Link](parent).add(child);
if ([Link](parent).size() > 2) return "false";
}

HashSet<Integer> allParents = new HashSet<>([Link]());


[Link](allChildren);

if ([Link]() != 1) return "false";


int root = [Link]().next();

if (hasCycle(root, parentToChildren, new HashSet<>())) return "false";


return "true";
}

private static boolean hasCycle(int node, HashMap<Integer, List<Integer>> tree,


HashSet<Integer> visited) {
if ([Link](node)) return true;
[Link](node);
if ([Link](node)) {
for (int child : [Link](node)) {
if (hasCycle(child, tree, visited)) return true;
}
}
return false;
}

public static void main(String[] args) {


[Link](arrayChallenge(new String[]{"(1,2)", "(2,4)", "(5,7)", "(7,2)", "(9,5)"})); // true
}
}
```

3. String Challenge

### Problem Statement


Convert a **written-out mathematical expression** into a **numeric expression**, evaluate it, and
return the result in words.
### Java Solution
```java
import [Link].*;

public class StringChallenge {


private static final Map<String, Integer> wordToDigit = [Link](
"zero", 0, "one", 1, "two", 2, "three", 3, "four", 4,
"five", 5, "six", 6, "seven", 7, "eight", 8, "nine", 9
);

public static String stringChallenge(String str) {


List<String> tokens = extractTokens(str);
int result = evaluateExpression(tokens);
return convertToWords(result);
}

private static List<String> extractTokens(String str) {


List<String> tokens = new ArrayList<>();
for (String word : [Link]()) {
while ([Link](word)) {
[Link](word);
str = [Link](word, "");
}
}
return tokens;
}

private static int evaluateExpression(List<String> tokens) {


int num = 0;
for (String token : tokens) {
num = num * 10 + [Link](token);
}
return num;
}

private static String convertToWords(int num) {


if (num < 0) return "negative" + convertToWords(-num);
return num == 0 ? "zero" : [Link]([Link](num).split("")).map(d ->
[Link]().stream().filter(e ->
[Link]().toString().equals(d)).findFirst().get().getKey()).reduce("", String::concat);
}

public static void main(String[] args) {


[Link](stringChallenge("onezeropluseight")); // oneeight
}
}
```

You might also like