diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_7.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_7.py new file mode 100644 index 00000000..f0ed2bb2 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_7.py @@ -0,0 +1,22 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + + answer = dict() + + for k, v in enumerate(nums): + + if v in answer: + return [answer[v], k] + else: + answer[target - v] = k + + return [] + + + + + + diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_7.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_7.py new file mode 100644 index 00000000..db19456c --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_7.py @@ -0,0 +1,22 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod +import re + +class Solution: + def isPalindrome(self, s: str) -> bool: + + # To lowercase + s = s.lower() + + # Remove non-alphanumeric characters + s = re.sub(pattern=r'[^a-zA-Z0-9]', repl='', string=s) + + # Determine if s is palindrome or not + len_s = len(s) + + for i in range(len_s//2): + + if s[i] != s[len_s - 1 - i]: + return False + + return True \ No newline at end of file diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_90_snakes_and_ladders.py b/src/my_project/interviews/top_150_questions_round_22/ex_90_snakes_and_ladders.py new file mode 100644 index 00000000..648b1c66 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_90_snakes_and_ladders.py @@ -0,0 +1,60 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod +from collections import deque, defaultdict + +class Solution: + def snakesAndLadders(self, board: List[List[int]]) -> int: + n = len(board) + + # Helper function to convert square number to (row, col) coordinates + def get_position(square): + # square is 1-indexed, convert to 0-indexed + square -= 1 + # Calculate row from bottom (0 is bottom row) + row = square // n + # Calculate column based on row direction + if row % 2 == 0: + # Even rows (from bottom): left to right + col = square % n + else: + # Odd rows (from bottom): right to left + col = n - 1 - (square % n) + # Convert to board coordinates (0 is top row in board) + return n - 1 - row, col + + # BFS to find shortest path + target = n * n + queue = deque([(1, 0)]) # (current_square, num_moves) + visited = {1} + + while queue: + curr, moves = queue.popleft() + + # Try all possible dice rolls (1 to 6) + for dice in range(1, 7): + next_square = curr + dice + + # Check if we've gone beyond the board + if next_square > target: + break + + # Get the board position for this square + r, c = get_position(next_square) + + # Check if there's a snake or ladder + if board[r][c] != -1: + next_square = board[r][c] + + # Check if we've reached the target + if next_square == target: + return moves + 1 + + # Add to queue if not visited + if next_square not in visited: + visited.add(next_square) + queue.append((next_square, moves + 1)) + + # If we can't reach the target + return -1 + + diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_90_snakes_and_ladders.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_90_snakes_and_ladders.ts new file mode 100644 index 00000000..26d0d0dd --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_90_snakes_and_ladders.ts @@ -0,0 +1,90 @@ +function snakesAndLadders(board: number[][]): number { + const n = board.length; + + // Helper function to convert square number to (row, col) coordinates + function getPosition(square: number): [number, number] { + // square is 1-indexed, convert to 0-indexed + square -= 1; + // Calculate row from bottom (0 is bottom row) + const row = Math.floor(square / n); + // Calculate column based on row direction + let col: number; + if (row % 2 === 0) { + // Even rows (from bottom): left to right + col = square % n; + } else { + // Odd rows (from bottom): right to left + col = n - 1 - (square % n); + } + // Convert to board coordinates (0 is top row in board) + return [n - 1 - row, col]; + } + + // BFS to find shortest path + const target = n * n; + const queue: [number, number][] = [[1, 0]]; // [current_square, num_moves] + const visited = new Set([1]); + + while (queue.length > 0) { + const [curr, moves] = queue.shift()!; + + // Try all possible dice rolls (1 to 6) + for (let dice = 1; dice <= 6; dice++) { + let nextSquare = curr + dice; + + // Check if we've gone beyond the board + if (nextSquare > target) { + break; + } + + // Get the board position for this square + const [r, c] = getPosition(nextSquare); + + // Check if there's a snake or ladder + if (board[r][c] !== -1) { + nextSquare = board[r][c]; + } + + // Check if we've reached the target + if (nextSquare === target) { + return moves + 1; + } + + // Add to queue if not visited + if (!visited.has(nextSquare)) { + visited.add(nextSquare); + queue.push([nextSquare, moves + 1]); + } + } + } + + // If we can't reach the target + return -1; +} + +// Test cases +console.log("Example 1:"); +const board1 = [ + [-1,-1,-1,-1,-1,-1], + [-1,-1,-1,-1,-1,-1], + [-1,-1,-1,-1,-1,-1], + [-1,35,-1,-1,13,-1], + [-1,-1,-1,-1,-1,-1], + [-1,15,-1,-1,-1,-1] +]; +console.log(`Input: board = ${JSON.stringify(board1)}`); +console.log(`Output: ${snakesAndLadders(board1)}`); // Expected: 4 +console.log(`Explanation: In the beginning, you start at square 1 (at row 5, column 0). +You decide to move to square 2 and must take the ladder to square 15. +You then decide to move to square 17 and must take the snake to square 13. +You then decide to move to square 14 and must take the ladder to square 35. +You then decide to move to square 36, ending the game. +This is the lowest possible number of moves to reach the last square, so return 4.`); + +console.log("\nExample 2:"); +const board2 = [[-1,-1],[-1,3]]; +console.log(`Input: board = ${JSON.stringify(board2)}`); +console.log(`Output: ${snakesAndLadders(board2)}`); // Expected: 1 + +export { snakesAndLadders }; + diff --git a/tests/test_150_questions_round_22/test_90_snakes_and_ladders_round_22.py b/tests/test_150_questions_round_22/test_90_snakes_and_ladders_round_22.py new file mode 100644 index 00000000..6ce5f898 --- /dev/null +++ b/tests/test_150_questions_round_22/test_90_snakes_and_ladders_round_22.py @@ -0,0 +1,48 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ + .ex_90_snakes_and_ladders import Solution +from typing import Optional, List + + +class SnakesAndLaddersTestCase(unittest.TestCase): + + def test_example_1(self): + """ + Example 1: + Input: board = [[-1,-1,-1,-1,-1,-1],[-1,-1,-1,-1,-1,-1],[-1,-1,-1,-1,-1,-1],[-1,35,-1,-1,13,-1],[-1,-1,-1,-1,-1,-1],[-1,15,-1,-1,-1,-1]] + Output: 4 + Explanation: + In the beginning, you start at square 1 (at row 5, column 0). + You decide to move to square 2 and must take the ladder to square 15. + You then decide to move to square 17 and must take the snake to square 13. + You then decide to move to square 14 and must take the ladder to square 35. + You then decide to move to square 36, ending the game. + This is the lowest possible number of moves to reach the last square, so return 4. + """ + solution = Solution() + board = [ + [-1, -1, -1, -1, -1, -1], + [-1, -1, -1, -1, -1, -1], + [-1, -1, -1, -1, -1, -1], + [-1, 35, -1, -1, 13, -1], + [-1, -1, -1, -1, -1, -1], + [-1, 15, -1, -1, -1, -1] + ] + expected = 4 + + result = solution.snakesAndLadders(board) + self.assertEqual(result, expected) + + def test_example_2(self): + """ + Example 2: + Input: board = [[-1,-1],[-1,3]] + Output: 1 + """ + solution = Solution() + board = [[-1, -1], [-1, 3]] + expected = 1 + + result = solution.snakesAndLadders(board) + self.assertEqual(result, expected) +