Repository navigation
Expand file tree
/
Copy pathWarmup.java
More file actions
87 lines (65 loc) · 2.4 KB
/
Copy pathWarmup.java
File metadata and controls
87 lines (65 loc) · 2.4 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import java.time.LocalDate;
import java.util.*;
public class Warmup {
public static void main(String[] args) {
//arrays
int[] arr = {5, 1, 2, 4};
List<Integer> list = new ArrayList<>();
for (int x : arr) {
list.add(x);
}
//sort
Collections.sort(list);
int[] sorted = list.stream().mapToInt(Integer::intValue).toArray();
//hashmap, frequency, sorting
String s = "interviewWarmup";
Map<Character, Integer> freqMap = new HashMap<>();
for (char ch : s.toCharArray()) {
freqMap.put(ch, freqMap.getOrDefault(ch, 0) + 1);
}
//sort by frequency
List<Map.Entry<Character, Integer>> entries = new ArrayList<>(freqMap.entrySet());
entries.sort((a, b) -> b.getValue() - a.getValue());
for (var entry : entries) {
System.out.println(entry.getKey() + " : " + entry.getValue());
}
//StringBuilder and conversions
//Task -> Reverse words in a sentence
String sentence = "This is a warmup exercise";
String[] words = sentence.split(" ");
StringBuilder sb = new StringBuilder();
for (int i = words.length - 1; i >= 0; i--) {
sb.append(words[i]);
if (i != 0) sb.append(" ");
}
String result = sb.toString();
System.out.println(result);
// Sliding window( Set + Two Pointers)
String str = "abcabcbb";
Set<Character> window = new HashSet<>();
int l = 0, mx = 0;
for (int r = 0; r < str.length(); r++) {
while (window.contains(str.charAt(r))) {
window.remove(str.charAt(l));
l++;
}
window.add(str.charAt(r));
mx = Math.max(mx, r - l + 1);
}
System.out.println("Longest substring without repeating characters: " + mx);
// Heap + K Largest Elements
int[] nums = {3, 2, 1, 5, 6, 4};
int k = 3;
PriorityQueue<Integer> pq = new PriorityQueue<>();
for (int i : nums) {
pq.offer(i);
if (pq.size() > k) pq.poll();
}
//Date + Integer/String Conversions
LocalDate date = LocalDate.parse("2026-01-08");
LocalDate newDate = date.plusDays(10);
int x = 12345;
String xS = String.valueOf(x);
int xsI = Integer.parseInt(xS);
}
}