Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 120 additions & 34 deletions src/main/java/CollectionsHackerrank/CollectionsHackerrankProblems.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
package CollectionsHackerrank;

import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.*;

public class CollectionsHackerrankProblems {
public class CollectionsHackerrankPractice {

public static class CollectionsHackerrankPractice {
public static void main(String[] args) {

List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(2);
list.add(3);
list.add(3);
list.add(3);
list.add(33);
list.add(33);
list.add(33);

System.out.println(list);
System.out.println(removeDuplicates(list));
System.out.println(countFrequency(list));
System.out.println(firstUnique(list));
// You can test your methods here

}
Expand All @@ -22,10 +34,7 @@ public static void main(String[] args) {
Output: [1,2,3,4,5]
*/
public static List<Integer> removeDuplicates(List<Integer> numbers) {

// TODO: Implement this method

return null;
return new ArrayList<>(new HashSet<>(numbers));
}

/*
Expand All @@ -37,10 +46,12 @@ public static List<Integer> removeDuplicates(List<Integer> numbers) {
Output: {1=1, 2=2, 3=3}
*/
public static Map<Integer, Integer> countFrequency(List<Integer> numbers) {
Map<Integer, Integer> map = new HashMap<>();

// TODO: Implement this method

return null;
for( int i : numbers){
map.put(i,map.getOrDefault(i,0)+1);
}
return map;
}

/*
Expand All @@ -52,9 +63,14 @@ public static Map<Integer, Integer> countFrequency(List<Integer> numbers) {
Output: 5
*/
public static Integer firstUnique(List<Integer> numbers) {

// TODO: Implement this method

Map<Integer,Integer> map = new LinkedHashMap<>();
for(int i: numbers){
map.put(i,map.getOrDefault(numbers,0));
}
for (Map.Entry<Integer,Integer>entry: map.entrySet())
if(entry.getValue()==1){
return entry.getKey();
}
return null;
}

Expand All @@ -69,8 +85,17 @@ public static Integer firstUnique(List<Integer> numbers) {
Output: true
*/
public static boolean twoSum(List<Integer> numbers, int target) {
Set<Integer> seen = new HashSet<>();

for (Integer num : numbers) {
int complement = target - num;

if (seen.contains(complement)) {
return true;
}

// TODO: Implement this method
seen.add(num);
}

return false;
}
Expand All @@ -84,10 +109,9 @@ public static boolean twoSum(List<Integer> numbers, int target) {
Output: 3
*/
public static int countUniqueWords(List<String> words) {
Set<String> uniqueWords = new HashSet<>(words);

// TODO: Implement this method

return 0;
return uniqueWords.size();
}

/*
Expand All @@ -100,9 +124,19 @@ public static int countUniqueWords(List<String> words) {
*/
public static Queue<Integer> reverseQueue(Queue<Integer> queue) {

// TODO: Implement this method
Stack<Integer> stack = new Stack<>();

return null;
// Move elements from queue to stack
while (!queue.isEmpty()) {
stack.push(queue.poll());
}

// Move elements back to queue (reversed order)
while (!stack.isEmpty()) {
queue.offer(stack.pop());
}

return queue;
}

/*
Expand All @@ -117,10 +151,27 @@ public static Queue<Integer> reverseQueue(Queue<Integer> queue) {
Output: false
*/
public static boolean isBalanced(String expression) {
//use a Stack.
//A stack works well because we must match every opening ( with a closing ) in the correct order.
Stack<Character> stack = new Stack<>();

// TODO: Implement this method
for (char ch : expression.toCharArray()) {

if (ch == '(') {
stack.push(ch); // store opening parenthesis
}

else if (ch == ')') {
if (stack.isEmpty()) {
return false; // no matching opening
}
stack.pop(); // match found
}
}

// if stack is empty, parentheses are balanced
return stack.isEmpty();

return false;
}

/*
Expand All @@ -132,10 +183,27 @@ public static boolean isBalanced(String expression) {
Output: 3
*/
public static Integer mostFrequent(List<Integer> numbers) {

// TODO: Implement this method

return null;
int max = numbers.getFirst();
int maxKey = numbers.getFirst();
Map<Integer, Integer> map = new HashMap<>();

for (Integer number : numbers) {
if (map.containsKey(number)) {
map.put(number, map.get(number) + 1);
}else{
map.put(number, 1);
}
}

for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
if (entry.getValue() > max) {
max = entry.getValue();
maxKey = entry.getKey();
}


}
return maxKey;
}

/*
Expand All @@ -152,10 +220,18 @@ public static Integer mostFrequent(List<Integer> numbers) {
}
*/
public static Map<Integer, List<String>> groupByLength(List<String> words) {
Map<Integer, List<String>> result = new HashMap<>();
for (String word : words) {
int length = word.length();

// TODO: Implement this method
if (!result.containsKey(length)) {
result.put(length, new ArrayList<>());
}

return null;
result.get(length).add(word);
}

return result;
}

/*
Expand All @@ -169,10 +245,20 @@ public static Map<Integer, List<String>> groupByLength(List<String> words) {
Output: 9
*/
public static int maxSlidingWindowSum(List<Integer> numbers, int k) {

// TODO: Implement this method

return 0;
if (numbers.size() < k){
return numbers.getFirst();
}
int max = Integer.MIN_VALUE;
for (int i = 0; i < numbers.size()-k+1; i++) {
int maxTest = 0;
for(int j = i; j < i+k; j++){
maxTest = numbers.get(j) + maxTest;
}
if (maxTest > max) {
max = maxTest;
}
}
return max;
}
}
}
}
22 changes: 17 additions & 5 deletions src/main/java/Hierachy/PRACTICE.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ B. Iterable
C. List
D. Queue

Answer: **B Iterable**
---

## Question 2
Expand All @@ -27,6 +28,7 @@ B. Collection
C. Map
D. Deque

Answer: **B Collection**
---

## Question 3
Expand All @@ -38,6 +40,8 @@ B. Set
C. Queue
D. Map

Answer: **D Map**

---

## Question 4
Expand All @@ -49,6 +53,7 @@ B. List
C. Queue
D. TreeSet

Answer: **B List**
---

## Question 5
Expand All @@ -60,17 +65,18 @@ B. Set
C. Queue
D. Map

Answer: **D Map**
---

# True or False

Write **True** or **False**.

1. `ArrayList` implements the `List` interface.
2. `Set` allows duplicate elements.
3. `Map` stores elements using keys and values.
4. `Queue` typically follows FIFO behavior.
5. `Iterable` allows collections to be used in enhanced for-loops.
1. `ArrayList` implements the `List` interface. TRUE
2. `Set` allows duplicate elements. FALSE
3. `Map` stores elements using keys and values. TRUE
4. `Queue` typically follows FIFO behavior. TRUE
5. `Iterable` allows collections to be used in enhanced for-loops. TRUE

---

Expand All @@ -80,12 +86,14 @@ Write **True** or **False**.

What is the difference between an **interface** and a **class** in the Java Collections Framework?

An interface defines behaviors(what can be done), and a class implements the behaviors and does the work.
---

## Question 2

Why is it recommended to declare variables using interfaces like `List` instead of concrete classes like `ArrayList`?

It proves better flexibility, code design, and Abstraction.
Example:

```java
Expand All @@ -98,6 +106,7 @@ List<String> list = new ArrayList<>();

Name three interfaces that extend `Collection`.

List, Set, and Queue
---

# Code Reading Exercise
Expand All @@ -118,8 +127,11 @@ for (String fruit : fruits) {
Questions:

1. Which interface type is used for the variable?
**List**
2. Which class is used as the implementation?
**ArrayList**
3. Why can the enhanced for-loop be used here?
**List extends Collections which extends iterable, so the enhanced for-loop can be used on all collections.**

---

Expand Down
12 changes: 9 additions & 3 deletions src/main/java/Iterable/Examples/ForEachLoopDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,25 @@ public static void main(String[] args) {
System.out.println("Printing students using a for-each loop:");

// TODO:
// Use a for-each loop to print each student name
for (String x: students){
System.out.println(x);

}

System.out.println("\nPrinting students in uppercase:");

for (String x: students){
System.out.println(x.toUpperCase());
}
// TODO:
// Use a for-each loop to print each name in uppercase


System.out.println("\nCount the number of students:");

int count = 0;

for (String x: students){
count=students.size();
}
// TODO:
// Use a for-each loop to count how many students are in the list

Expand Down
14 changes: 13 additions & 1 deletion src/main/java/Iterable/Examples/IteratorDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ public static void main(String[] args) {
// TODO:
// Use iterator.hasNext() and iterator.next()
// Print each number
while(iterator.hasNext()){
int x = iterator.next();
System.out.println(x);

}




System.out.println("\nRemoving odd numbers using Iterator");
Expand All @@ -35,7 +42,12 @@ public static void main(String[] args) {
// TODO:
// Use iterator to remove odd numbers
// Remember: use iterator.remove()

while(iterator.hasNext()){
int x = iterator.next();
if (x%2==0){
iterator.remove();
}
}

System.out.println("\nUpdated list:");
System.out.println(numbers);
Expand Down
Loading