【4】阿里面试题整理

1. 介绍一下数据库死锁

数据库死锁是指两个或多个事务,由于互相请求对方持有的资源而造成的互相等待的状态,导致它们都无法继续执行。

死锁会导致事务阻塞,系统性能下降甚至应用崩溃。

比如:事务T1持有资源R1并等待R2,事务T2持有R2并等待R1,这就形成了一个循环等待,导致死锁。

2. 手撕:快排

java 复制代码
public class QuickSort {

    public static void quickSort(int[] arr, int low, int high) {
        if (low < high) {
            int pivotIndex = partition(arr, low, high);
            quickSort(arr, low, pivotIndex - 1);
            quickSort(arr, pivotIndex + 1, high);
        }
    }

    // 分区函数,以最左边的元素为基准元素
    private static int partition(int[] arr, int low, int high) {
        int pivot = arr[low];  // 选择最左边的元素作为基准
        int i = low;    // i 指向小于基准的区域的末尾,初始指向最左边
        for (int j = low + 1; j <= high; j++) { // j从low+1 开始遍历
            if (arr[j] < pivot) {
                i++;
                swap(arr, i, j); // 将小于基准的元素交换到左侧
            }
        }
        swap(arr, low, i); // 将基准元素交换到正确的位置
        return i;           // 返回基准元素的索引
    }



    // 交换数组中两个元素
    private static void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
    
}

3. 手撕:二叉树的中序遍历

java 复制代码
public class InorderTraversal {
    // 定义二叉树节点
    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int val) {
            this.val = val;
            this.left = null;
            this.right = null;
        }
    }

    // 中序遍历
    public static void inorderTraversal(TreeNode root) {
        if (root != null) {
            inorderTraversal(root.left);   // 遍历左子树
            System.out.print(root.val + " ");  // 访问根节点
            inorderTraversal(root.right);  // 遍历右子树
        }
    }
}

4. 手撕:最大子数组和

java 复制代码
public class MaxSubarraySum {
    public static int maxSubArray(int[] nums) {
        // 判断输入数组是否为空或长度为0
        if (nums == null || nums.length == 0) {
            return 0; // 空数组或 null 返回0
        }

        // 记录全局最大子数组和
        int maxGlobalSum = nums[0];
        // 记录以当前元素结尾的最大子数组和
        int maxCurrentSum = nums[0];

        // 遍历数组,从第二个元素开始
        for (int i = 1; i < nums.length; i++) {
            // 更新以当前元素结尾的最大子数组和
            // 取当前元素值与当前元素加上以前一个元素结尾的最大子数组和中的较大值
            maxCurrentSum = Math.max(nums[i], maxCurrentSum + nums[i]);
            // 更新全局最大子数组和
            // 取全局最大子数组和与以当前元素结尾的最大子数组和中的较大值
            maxGlobalSum = Math.max(maxGlobalSum, maxCurrentSum);
        }

        // 返回全局最大子数组和
        return maxGlobalSum;
    }


}
相关推荐
geovindu21 分钟前
python:DecisionTree
开发语言·后端·python·算法
Cosolar26 分钟前
从 Java 21 升到 25、Spring Boot 3 升 4:一次可复现的迁移踩坑实录
java·后端·面试
geovindu2 小时前
python: Decision Tree Regressor
开发语言·后端·python·算法·决策树
IT空门:门主2 小时前
LangChain4j 1.24.0 深度解读:新特性、版本对比与学习路线
java·学习
用户3721574261354 小时前
Java 批量操作 Excel 超链接:添加、读取、修改和删除
java
AllData公司负责人4 小时前
实战二:AllData大模型与AI数据底座-数据传输平台-DBSyncer,搭建 Http 接口同步至 Doris,API 业务数据自动拉取入数仓
大数据·数据库·数据仓库·人工智能·算法·http·doris
用户3721574261355 小时前
Java 将 Excel 图表导出为图片:指定图表、批量导出与图表工作表
java
武子康5 小时前
火系被选七成,就应该削弱吗?
人工智能·算法·机器学习
炸膛坦客6 小时前
单片机/C/C++八股:(三十七)指针数组 和 数组指针
c语言·开发语言·c++
这个昵称不容易6 小时前
交流论坛网站设计与实现:SpringBoot 论坛 130 个接口,附论文与运行截图
java·springboot·毕设