0% found this document useful (0 votes)
19 views1 page

Min Stack and Array Intersection Solutions

The document contains three coding problems with solutions and explanations. The first problem is a Min Stack implementation that allows tracking of minimum values, the second problem finds the intersection of two arrays using HashSet, and the third problem reverses a string using a two-pointer approach. Each section includes code, an explanation of the logic, and a dry run example.

Uploaded by

Riya Negi.57
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)
19 views1 page

Min Stack and Array Intersection Solutions

The document contains three coding problems with solutions and explanations. The first problem is a Min Stack implementation that allows tracking of minimum values, the second problem finds the intersection of two arrays using HashSet, and the third problem reverses a string using a two-pointer approach. Each section includes code, an explanation of the logic, and a dry run example.

Uploaded by

Riya Negi.57
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

1.

Min Stack
Code:
class MinStack { private Stack stack; private Stack minStack; public MinStack() {
stack = new Stack<>(); minStack = new Stack<>(); } public void push(int val) {
[Link](val); if ([Link]() || val <= [Link]()) {
[Link](val); } } public void pop() { if
([Link]().equals([Link]())) { [Link](); } [Link](); } public int
top() { return [Link](); } public int getMin() { return [Link](); } }

Explanation:
We use two stacks: one for storing all elements, and another for tracking minimum values. - push():
adds element to both stacks if it is the new minimum. - pop(): removes from both if it is the minimum. -
top(): returns top of main stack. - getMin(): returns minimum element from minStack.
Dry Run:
Example: push(5), push(3), push(7), push(2) - After push(5): stack=[5], minStack=[5] - After push(3):
stack=[5,3], minStack=[5,3] - After push(7): stack=[5,3,7], minStack=[5,3] - After push(2):
stack=[5,3,7,2], minStack=[5,3,2] - getMin() = 2

2. Intersection of Two Arrays


Code:
class Solution { public int[] intersection(int[] nums1, int[] nums2) { Set set1 = new
HashSet<>(); Set result = new HashSet<>(); for (int num : nums1) { [Link](num); }
for (int num : nums2) { if ([Link](num)) { [Link](num); } } int[] ans =
new int[[Link]()]; int i = 0; for (int num : result) { ans[i++] = num; } return
ans; } }

Explanation:
We use HashSet to remove duplicates and check intersections. - First, add all nums1 elements to set1.
- Traverse nums2, check if element exists in set1. If yes, add to result set. - Convert result set to array
and return.
Dry Run:
nums1 = [1,2,2,1], nums2 = [2,2] - set1 = {1,2} - Traverse nums2: 2 exists in set1 → add to result {2} -
Final answer = [2]

3. Reverse String
Code:
class Solution { public void reverseString(char[] s) { int left = 0, right = [Link]
- 1; while (left < right) { char temp = s[left]; s[left] = s[right]; s[right] = temp;
left++; right--; } } }

Explanation:
We use two pointers from left and right to swap elements until they meet in the middle.
Dry Run:
Input: ['h','e','l','l','o'] - Swap h and o → ['o','e','l','l','h'] - Swap e and l → ['o','l','l','e','h'] Final Output:
['o','l','l','e','h']

You might also like