Code Monkey home page Code Monkey logo

java-interview's Issues

进一步完善

  1. IO
  2. 多线程编程题
  3. JVM调优例子
  4. Redis数据结构原理
  5. SQL优化加强
  6. 项目优化
  7. 套路:针对

数据库-MySQL SQL 优化 答案不准确

问:有个表特别大,字段是姓名、年龄、班级,如果调用select * from table where name = xxx and age = xxx该如何通过建立索引的方式优化查询速度?
答:由于mysql查询每次只能使用一个索引,如果在name、age两列上创建复合索引的话将带来更高的效率。如果我们创建了(name, age)的复合索引,那么其实相当于创建了(name, age)、(name)三个索引,这被称为最佳左前缀特性。因此我们在创建复合索引时应该将最常用作限制条件的列放在最左边,依次递减。其次还要考虑该列的数据离散程度,如果有很多不同的值的话建议放在左边,name的离散程度也大于age。

这里的 (name,age) 索引使用的不是最左前缀原则,而是索引下推
最左前缀原则用于 name like 'A%'这样的查询,会使用到索引,加速查询
索引下推是指在索引遍历的过程中优先对索引中包含的字段做判断,不满足条件的直接过滤,减少回表次数

约瑟夫环

题目:

有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的哪一位?

import java.util.Scanner;

public class Test {
static int n; //从屏幕输入固定的人数n;
static int[] people; //人的数组;

public static void main(String[] args) {
    System.out.println("请输入人数n:");
    Scanner input = new Scanner(System.in);
    n = input.nextInt();
    people = new int[n];
    //开始非3的数设置报数
    selectNoThreeToSet();
    //打印结果
    printResult();
}

//开始非3的数设置报数
private static void selectNoThreeToSet() {
    int notThreeCount = n, num = 1;      //不是3的个数,用来作为一个跳出循环的条件,当notThreeCount=1时,跳出循环,代表只剩一个了
    for (int i = 0; ; i++) {
        if (i == n)                //检索不是只遍历一次数组,需要循环遍历,当下标i自加到数组最后时,需要从0开始自加 ,多次遍历
        {
            i = 0;
        }
        if (people[i] != 3) {     //只有不为3的进行赋值
            if (num > 3)          //报号的数,循环123,如果大于3即4,重新从1开始
            {
                num = 1;
            }
            people[i] = num;
            if (num == 3)      //当只有报数为3时notThreeCount自减
            {
                notThreeCount--;
            }
            num++;              //如果一个同学报数成功,下一个需要加1(这里不考虑循环回1)
        }
        if (notThreeCount == 1) {
            break;
        }
    }
}

//打印结果
private static void printResult() {
    int t = 0;
    for (int i = 0; i < n; i++) {
        System.out.print(people[i] + "\t");
        if (people[i] != 3) {
            t = i;
        }
    }
    System.out.printf("\n最后剩下的数为原来第%d号的同学\n", t + 1);
}

}

public class Test {
public static int findSurvivor(int n, int m) {
List people = new ArrayList<>();
for (int i = 1; i <= n; i++) {
people.add(i); // 初始化列表,包含1到n的人
}

    while (people.size() > 1) {
        for (int j = 0; j < m - 1; j++) {
            people.add(people.remove(0));  // 移除第0个元素并添加到列表末尾
        }
        people.remove(0);  // 移除第0个元素,即报数到m的人
    }

    return people.get(0);  // 返回最后剩下的那个人的原始编号
}

public static void main(String[] args) {
    int n = 10;  // 人数
    int m = 3;   // 报数上限
    System.out.println("最后剩下的是原来第 " + findSurvivor(n, m) + " 号的人");
}

}

错别字

ThreadLocal
使用ThreadLocal userInfo = new ThreadLocal()的方式,让每个线程内部都会维护一个ThreadLocalMap,里边包含若干了 Entry(K-V 键值对),每次存取都会先的都当前线程,然后得到该线程对象中的Map,然后与Map交互。

此处有错别字

支持你

的确问的都是这些,哈哈

反转二叉树写的有问题吧

如题,反转打印那里写的不对

TreeNode currentLineRightestNode = this;
.....

if (currentNode.value == currentLineRightestNode.value) {
                System.out.println();
                currentLineRightestNode.value = nextLineRightestNode.value;
            }
currentLineRightestNode.value = nextLineRightestNode.value;

这样赋值之后,TreeNode 不就被修改了吗?

二面补充

数据库主从延迟同步方案、大数据量实时写查如何实现
强一致如何实现
kafka架构、顺序消息如何实现
IM架构如何实现,消息保存在哪里,从哪里拉数据

todo

http://118.25.23.115/
https://tech.meituan.com/2014/08/20/innodb-lock.html
https://zhuanlan.zhihu.com/p/67542731
B与B+区别:

  1. b+树的中间节点不保存数据,所以磁盘页能容纳更多节点元素;
  2. b+树查询必须查找到叶子节点,b树只要匹配到即可不用管元素位置,因此b+树查找更稳定
  3. 对于范围查找来说,b+树只需遍历叶子节点链表即可,b树却需要重复地中序遍历

压缩链表原理:
在内存中是连续存储的,但是不同于数组,为了节省内存,ziplist的每个元素所占的内存大小可以不同
ziplist将一些必要的偏移量信息记录在了每一个节点里,使之能跳到上一个节点或下一个节点

与跳表应用场景:
当zset满足以下两个条件的时候,使用ziplist:

  1. 保存的元素少于128个
  2. 保存的所有元素大小都小于64字节

HashMap rehash过程:

  1. 空间不够用了,所以需要分配一个大一点的空间,然后保存在里面的内容需要重新计算 hash
  2. 如果两个线程都发现HashMap需要重新调整大小了,它们会同时试着调整大小
  3. 在调整大小的过程中,存储在链表中的元素的次序会反过来,因为移动到新的bucket位置的时候,HashMap并不会将元素放在链表的尾部,而是放在头部,这是为了避免尾部遍历(tail traversing)。如果条件竞争发生了,那么就死循环了

redis数据过期策略:
定时过期:每个设置过期时间的key都需要创建一个定时器,到过期时间就会立即清除
惰性过期:只有当访问一个key时,才会判断该key是否已过期,过期则清除
定期过期:每隔一定的时间,会扫描一定数量的数据库的expires字典中一定数量的key,并清除其中已过期的key

内存淘汰策略:
no-eviction:当内存不足以容纳新写入数据时,新写入操作会报错
allkeys-lru:当内存不足以容纳新写入数据时,在键空间中,移除最近最少使用的key
allkeys-random:当内存不足以容纳新写入数据时,在键空间中,随机移除某个key
volatile-lru:当内存不足以容纳新写入数据时,在设置了过期时间的键空间中,移除最近最少使用的key
volatile-random:当内存不足以容纳新写入数据时,在设置了过期时间的键空间中,随机移除某个key
volatile-ttl:当内存不足以容纳新写入数据时,在设置了过期时间的键空间中,有更早过期时间的key优先移除

有序数组中位数
https://leetcode-cn.com/problems/median-of-two-sorted-arrays/solution/xun-zhao-liang-ge-you-xu-shu-zu-de-zhong-wei-s-114/

多线程交替打印alibaba:
https://blog.csdn.net/CX610602108/article/details/106427979
我自行实现的简单解法:

public class Test {
    private int currentI = 0;

    private synchronized void printSpace(int inputLength) {
        while (true) {
            if (currentI == inputLength) {
                currentI = 0;
                System.out.print(" ");
                this.notifyAll();
            } else {
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private synchronized void printLetter(int i, char c) {
        while (true) {
            if (i == currentI) {
                currentI++;
                System.out.print(c);
                this.notifyAll();
            } else {
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private synchronized void print(String input) throws Exception {
        Test test = new Test();
        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            int finalI = i;
            Thread tt = new Thread(() -> test.printLetter(finalI, c));
            tt.start();
        }
        Thread space = new Thread(() -> test.printSpace(input.length()));
        space.start();
        space.join();
    }


    public static void main(String[] args) throws Exception {
        Test test = new Test();
        test.print("alibaba");
    }
}

数组中的k个最小值:
https://leetcode-cn.com/problems/zui-xiao-de-kge-shu-lcof/solution/zui-xiao-de-kge-shu-by-leetcode-solution/

todo

/*
二十进制相加
sample
input

1234567890+abcdefghij
99999jjjjj+9999900001

output

bdfi02467j
iiiij00000
*/

项目中的设计模式:状态机
https://www.cnblogs.com/xyzq/p/11090344.html

多叉树中最长的连续序列:
https://gist.github.com/thanlau/34bd056dd0b1662d5fd483e157a27dfb

/**

  • Definition for a multi tree node.

  • public class MultiTreeNode {

  • int val;
    
  • List<MultiTreeNode> children;
    
  • MultiTreeNode(int x) { val = x; }
    
  • }
    /
    public class Solution {
    /
    *

    • @param root the root of k-ary tree
    • @return the length of the longest consecutive sequence path
      */
      int max = 0;

    public int longestConsecutive3(MultiTreeNode root) {
    // Write your code here
    if (root == null){
    return 0;
    }
    helper(root);
    return max;
    }

    public int[] helper(MultiTreeNode root) {
    int up = 0;
    int down = 0;
    if (root == null){
    return new int[]{down, up};
    }
    for ( MultiTreeNode node : root.children ){
    int[] h = helper(node);
    if (node.val == root.val + 1){
    down = Math.max(down, h[0] + 1);
    }
    if (node.val + 1 == root.val){
    up = Math.max(up, h[1] + 1);
    }
    }
    max = Math.max(max, down + up + 1);
    return new int[]{down, up};
    }
    }

https://crossoverjie.top/%2F2017%2F08%2F11%2Fsbc4%2F

重排链表

count1count*count字段区别

Redis强化

  1. 底层数据结构(Dict、值的结构)
  2. 集群模式(介绍、优缺点、Lua限制、客户端重试、一致性hash)
  3. 缓存常用使用方式(缓存读不到就从数据库读的问题、重要数据写到缓存、一致性问题)

Some suggestions

😄 这是一个很不错的 repo。

  • 关于高并发、分布式、高可用这一块的相关面试题及相关剖析,可以参考这里
  • 文档的书写还是要规范一些,比如中文与数字之间用空格隔开。

cannot load picture

after cloning the project,
pictures cannot be accessed via local markdown file

新增阿里面试点

  1. 各种锁(自旋锁、轻量锁...)的理解《深入理解Java虚拟机》
  2. IOC / DI 深入理解
  3. 多线程CAS深入理解

双亲委派模型

在jvm那个章节中 关于破坏双亲委派的链接栗子给的不合理。栗子只是展示了怎么自定义一个类加载器而没有正真意义上去破坏双亲委派模型。不是简单的去重写findCLass就完事了。建议换个好一点的栗子。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.