What test cases should be considered for the join method? What optimizations can be added?
Kotlin
// book(seat_id, user_id) PostgreSQL Begin Transaction(read_commited) seat = select * from seats where id = seat_id for update if(seat == null seat.status == "booked") rollback return error("Seat isnt available") insert into bookings (user_id, seat_id, status, created_at) values (used_id, ...) update seats set status = 'booked' where id = seat_id commit return success()
Why is the closing bracket the key in the Map, and the opening bracket the value? What will change if you do the opposite? What is the complexity of containsValue vs get by key?
/* SELECT left.K, left.V1, right.V2 FROM left JOIN right ON left.K = right.K; */ public <K extends Comparable<K>, V1, V2> List<Triple<K, V1, V2>> join(List<Pair<K, V1>> left, List<Pair<K, V2>> right) { return null; } private static class Pair<A, B> { private A first; private B second; Pair(A first, B second) { this.first = first; this.second = second; } } private static class Triple<A, B, C> { private A first; private B second; private C third; Triple(A first, B second, C third) { this.first = first; this.second = second; this.third = third; } }
/*Given a string, implement a function to check the correctness of bracket notation. Each opening bracket must have a corresponding closing bracket. Opening and closing brackets must be in the correct order. Types of brackets: "{", "}", "(", ")", "[", "]". Input: "([]){}[(()())()]" Result: true Input: "[(])" Result: false Because brackets 2 and 3 are incorrect, the closing ']' appears before the closing '('.*/ public boolean check(String s){ Deque<Character> stack = new ArrayDeque<>(); Map<Character, Character> map = Map.of( ')', '(', ']', '[', '}', '{' ); // ((( for( char c : s.toCharArray()){ if(map.containsValue(c)){ stack.push(c); } else if(map.containsKey(c)){ if( stack.isEmpty() || stack.pop() != map.get(c)){ return false; } } } return stack.isEmpty(); }