Untitled
user_0606344
java
9 days ago
4.2 kB
5
Indexable
Never
import java.util.Stack; // Q1 public class Solution { public static boolean solution(String input) { Stack<Character> stack = new Stack<>(); for (int i = 0; i < input.length(); i++) { char ch = input.charAt(i); // If the character is a left parenthesis, push it onto the stack if (ch == '(') { stack.push(ch); } // If the character is a right parenthesis, check the stack else if (ch == ')') { // If the stack is empty, it means there is no matching left parenthesis if (stack.isEmpty()) { return false; } // Otherwise, pop the top of the stack stack.pop(); } } // In the end, the stack should be empty if all parentheses are balanced return stack.isEmpty(); } public static void main(String[] args) { // Test cases System.out.println(solution("(Hello) OR (World)")); // true System.out.println(solution("(Hello)")); // true System.out.println(solution("())(()")); // false System.out.println(solution("(content:this OR name:this) AND (content:that OR name:that)")); // true } } // Q2 import java.util.Arrays; public class PowerOfTwoCounter { public static int countPowersOfTwo(long[] arr) { int count = 0; long powerOfTwo = 1; while (powerOfTwo <= arr[arr.length - 1]) { // Use binary search to find powerOfTwo in arr if (Arrays.binarySearch(arr, powerOfTwo) >= 0) { count++; } // Move to the next power of two powerOfTwo *= 2; } return count; } public static void main(String[] args) { long[] arr = {1, 2, 3, 4, 8, 16, 32, 64, 128, 256}; System.out.println(countPowersOfTwo(arr)); // Output should be 9 } } //Q3 import java.util.*; import java.util.concurrent.*; import java.util.concurrent.atomic.*; class Solution { public static class RequestCounter { // Enum to represent the applications public static enum Application { APP_1, APP_2, APP_3, APP_4; } // Map to store request counts per application private final ConcurrentHashMap<Application, AtomicLong> requestCounts; // Set of valid applications private final Set<Application> validApplications; // Constructor to initialize the valid applications and request counts public RequestCounter(Collection<Application> validApplications) { this.validApplications = new HashSet<>(validApplications); this.requestCounts = new ConcurrentHashMap<>(); // Initialize the request counts for each valid application for (Application app : validApplications) { requestCounts.put(app, new AtomicLong(0)); } } // Method to count a request for a given application public void countRequest(Application sourceApplication) { if (validApplications.contains(sourceApplication)) { requestCounts.get(sourceApplication).incrementAndGet(); } } // Method to get the set of valid applications public Set<Application> getApplications() { return Collections.unmodifiableSet(validApplications); } // Method to get the total number of requests across all applications public long getTotalRequests() { long total = 0; for (AtomicLong count : requestCounts.values()) { total += count.get(); } return total; } // Method to get the number of requests for a specific application public long getRequestsByApplication(Application sourceApplication) { AtomicLong count = requestCounts.get(sourceApplication); return count != null ? count.get() : 0; } } public static void main(String[] args) { // This exercise is not intended to be run } }
Leave a Comment