当前位置:   article > 正文

Java架构-三天拿到阿里、头条跟美团的offer,我做了这些准备

java 美团 阿里怎么选

1 回顾
透露一下,本人是双非二本,自从高考失利以后还以为自己要一直这么平凡下去,没想到过了三年终于又给我一个机会让我重新证明了自己,能给我去阿里、头条跟美团的面试机会,最后选择去到阿里这样的大厂工作,真的倍感荣幸,如果喜欢我的文章别忘了给我点个赞转发跟评论,拜托了
2 把自己训练成HashMap和复读机
这几次面试给我最大的感触就是,当你觉得自己像复读机能把面试题给复读出来并且对面试官所提的问题能像HashMap一样在常数时间内找到答案的时候,你就离成功很近了.
下面是我在准备面试的时候收集的一些知识点:
2.1 Java
2.1.1 volatile理解,JMM中主存和工作内存到底是啥?和JVM各个部分怎么个对应关系?
参考link
2.1.2 序列化
Serializable在序列化时使用了反射,从而导致GC的频繁调用,参考link
2.1.3 可见性,原子性,有序性(必考)

  • 可见性volatile,一个线程的修改对另外一个线程是马上可见的,
  • 原子性CAS操作,要么都做要么都不做
  • 有序性synchronized通过进入和退出Monitor(观察器)实现,CPU可能会乱序执行指令,如果在本线程内观察,所有操作都是有序的,如果在一个线程中观察另一个线程,所有操作都是无序的.参考link

2.1.4 Java锁机制
java锁机制其实是锁总线,同步关键字和Lock接口的优劣.
2.1.5 Java的常量池?不同String赋值方法,引用是否相等?
字面值是常量,在字节码中使用id索引,equals相等引用不一定相等,Android上String的构造函数会被虚拟机拦截,重定向到StringFactory
2.1.6 HashMap的实现?树化阈值?负载因子?
数组加链表加红黑树,默认负载因子0.75,树化阈值8,这部分比较常考,建议专门准备.(打个小广告OWO,你也可以关注我的专栏,里面有一篇文章分析了HashMap和ArrayMap)
2.1.7 Java实现无锁同步
CAS的实现如AtomicInteger等等
2.1.8 单例模式

  • 双重检查

  1. public class Singleton {
  2. private static volatile Singleton singleton;
  3. private Singleton() {}
  4. public static Singleton getInstance() {
  5. if (singleton == null) {
  6. synchronized (Singleton.class) {
  7. if (singleton == null) {
  8. singleton = new Singleton();
  9. }
  10. }
  11. }
  12. return singleton;
  13. }
  14. }
  15. 复制代码

  • 反序列化安全,反射安全 枚举级单例,类加载时由JVM保证单例,反序列化不会生成新对象,另外一种反射安全是在构造函数中对单例进行检查如果存在则抛出异常.

2.1.9 锁的条件变量
信号量要与一个锁结合使用,当前线程要先获得这个锁,然后等待与这个锁相关联的信号量,此时该锁会被解锁,其他线程可以抢到这个锁,如果其他线程抢到了这个锁,那他可以通知这个信号量,然后释放该锁,如果此时第一个线程抢到了该锁,那么它将从等待处继续执行(应用场景,将异步回调操作封装为变为同步操作,避免回调地狱)
信号量与锁相比的应用场景不同,锁是服务于共享资源的,而信号量是服务于多个线程间的执行的逻辑顺序的,锁的效率更高一些.
2.1.10 ThreadLocal原理
线程上保存着ThreadLocalMap,每个ThreadLocal使用弱引用包装作为Key存入这个Map里,当线程被回收或者没有其他地方引用ThreadLocal时,ThreadLocal也会被回收进而回收其保存的值
2.1.11 软引用,弱引用,虚引用

  • 软引用内存不够的时候会释放
  • 弱引用GC时释放
  • 虚引用,需要和一个引用队列联系在一起使用,引用了跟没引用一样,主要是用来跟GC做一些交互.

2.1.12 ClassLoader双亲委派机制
简单来说就是先把加载请求转发到父加载器,父加载器失败了,再自己试着加载
2.1.13 GC Roots有这些
通过System Class Loader或者Boot Class Loader加载的class对象,通过自定义类加载器加载的class不一定是GC Root:

  • 处于激活状态的线程
  • 栈中的对象
  • JNI栈中的对象
  • JNI中的全局对象
  • 正在被用于同步的各种锁对象
  • JVM自身持有的对象,比如系统类加载器等。

2.1.14 GC算法

2.2 ACM
对于ACM,比较常考链表的题,不常刷算法的同学一定不要对其有抵触心理.
你可能会问为什么要ACM?网上答案说的什么提高代码质量,能够更好地阅读别人的代码这些理由有一定道理,但对于我们去面试的人而言最重要的是ACM是面试官考察你编码能力的最直接的手段,所以不用说这么多废话刷题就够了.
刷题的话,建议去刷leetcode,题号在200以内的,简单和中等难度,不建议刷困难,因为面试的时候基本就不会出,没人愿意在那里等你想一个半个小时的.
在面试官面前现场白板编程时,可以先把思路告诉面试官,写不写得出来是另外一回事,时间复杂度和空间复杂度是怎么来的一定要搞清楚.在编码时也不一定要写出最佳的时间和空间的算法,但推荐你写出代码量最少,思路最清晰的,这样面试官看得舒服,你讲得也舒服.
下面是我在网上收集或者是在实际中遇到过的ACM题,基本上在leetcode上也都有类似的.
2.2.1 数组、链表

  • 链表逆序(头条几乎是必考的)
  1. public ListNode reverseList(ListNode head)
  2. {
  3. if (head == null)
  4. {
  5. return null;
  6. }
  7. if (head.next == null)
  8. {
  9. return head;
  10. }
  11. ListNode prev = null;
  12. ListNode current = head;
  13. while (current != null)
  14. {
  15. ListNode next = current.next;
  16. current.next = prev;
  17. prev = current;
  18. current = next;
  19. }
  20. return prev;
  21. }
  22. 复制代码

删除排序数组中的重复项

  1. public int removeDuplicates(int[] nums)
  2. {
  3. int length = nums.length;
  4. if (length == 0 || length == 1)
  5. {
  6. return length;
  7. }
  8. int size = 1;
  9. int pre = nums[0];
  10. for (int i = 1; i < length; )
  11. {
  12. if (nums[i] == pre)
  13. {
  14. i++;
  15. } else
  16. {
  17. pre = nums[size++] = nums[i++];
  18. }
  19. }
  20. return size;
  21. }
  22. 复制代码
  • 数组中找到重复元素
  • n个长为n的有序数组,求最大的n个数
  • 用O(1)的时间复杂度删除单链表中的某个节点 把后一个元素赋值给待删除节点,这样也就相当于是删除了当前元素,只有删除最后一个元素的时间为o(N)平均时间复杂度仍然为O(1)
  1. public void deleteNode(ListNode node) {
  2. ListNode next = node.next;
  3. node.val = next.val;
  4. node.next = next.next;
  5. }
  6. 复制代码

删除单链表的倒数第N个元素 两个指针,第一个先走N步第二个再走,时间复杂度为O(N),参考link

  1. public ListNode removeNthFromEnd(ListNode head, int n) {
  2. if (head == null)
  3. {
  4. return null;
  5. }
  6. if (head.next == null)
  7. {
  8. return n == 1 ? null : head;
  9. }
  10. int size = 0;
  11. ListNode point = head;
  12. ListNode node = head;
  13. do
  14. {
  15. if (size >= n + 1)
  16. {
  17. point = point.next;
  18. }
  19. node = node.next;
  20. size++;
  21. } while (node != null);
  22. if (size == n)
  23. {
  24. return head.next;
  25. }
  26. node = point.next;
  27. point.next = node == null ? null : node.next;
  28. return head;
  29. }复制代码
  • 从长序列中找出前K大的数字
  • 用数组实现双头栈
  1. public static class Stack<T>
  2. {
  3. public Stack(int cap)
  4. {
  5. if (cap <= 0)
  6. {
  7. throw new IllegalArgumentException();
  8. }
  9. array = new Object[cap];
  10. left = 0;
  11. right = cap - 1;
  12. }
  13. private Object[] array;
  14. private int left;
  15. private int right;
  16. public void push1(T val)
  17. {
  18. int index = left + 1;
  19. if (index < right)
  20. {
  21. array[index] = val;
  22. }
  23. left = index;
  24. }
  25. @SuppressWarnings("unchecked")
  26. public T pop1()
  27. {
  28. if (left > 0)
  29. {
  30. return (T)array[left--];
  31. }
  32. return null;
  33. }
  34. public void push2(T val)
  35. {
  36. int index = right - 1;
  37. if (index > left)
  38. {
  39. array[index] = val;
  40. }
  41. right = index;
  42. }
  43. @SuppressWarnings("unchecked")
  44. public T pop2()
  45. {
  46. if (right < array.length)
  47. {
  48. return (T)array[right++];
  49. }
  50. return null;
  51. }
  52. }复制代码

两个链表求和,返回结果也用链表表示 1 -> 2 -> 3 + 2 -> 3 -> 4 = 3 -> 5 -> 7

  1. public ListNode addTwoNumbers(ListNode node1, ListNode node2)
  2. {
  3. ListNode head = null;
  4. ListNode tail = null;
  5. boolean upAdd = false;
  6. while (!(node1 == null && node2 == null))
  7. {
  8. ListNode midResult = null;
  9. if (node1 != null)
  10. {
  11. midResult = node1;
  12. node1 = node1.next;
  13. }
  14. if (node2 != null)
  15. {
  16. if (midResult == null)
  17. {
  18. midResult = node2;
  19. } else
  20. {
  21. midResult.val += node2.val;
  22. }
  23. node2 = node2.next;
  24. }
  25. if (upAdd)
  26. {
  27. midResult.val += 1;
  28. }
  29. if (midResult.val >= 10)
  30. {
  31. upAdd = true;
  32. midResult.val %= 10;
  33. }
  34. else
  35. {
  36. upAdd = false;
  37. }
  38. if (head == null)
  39. {
  40. head = midResult;
  41. tail = midResult;
  42. } else
  43. {
  44. tail.next = midResult;
  45. tail = midResult;
  46. }
  47. }
  48. if (upAdd)
  49. {
  50. tail.next = new ListNode(1);
  51. }
  52. return head;
  53. }
  54. 复制代码

交换链表两两节点

  1. public ListNode swapPairs(ListNode head)
  2. {
  3. if (head == null)
  4. {
  5. return null;
  6. }
  7. if (head.next == null)
  8. {
  9. return head;
  10. }
  11. ListNode current = head;
  12. ListNode after = current.next;
  13. ListNode nextCurrent;
  14. head = after;
  15. do
  16. {
  17. nextCurrent = after.next;
  18. after.next = current;
  19. if (nextCurrent == null)
  20. {
  21. current.next = null;
  22. break;
  23. }
  24. current.next = nextCurrent.next;
  25. after = nextCurrent.next;
  26. if (after == null)
  27. {
  28. current.next = nextCurrent;
  29. break;
  30. }
  31. current = nextCurrent;
  32. } while (true);
  33. return head;
  34. }
  35. 复制代码

找出数组中和为给定值的两个元素,如:[1, 2, 3, 4, 5]中找出和为6的两个元素。

  1. public int[] twoSum(int[]mun,int target)
  2. {
  3. Map<Integer, Integer> table = new HashMap<>();
  4. for (int i = 0; i < mun.length; ++i)
  5. {
  6. Integer value = table.get(target - mun[i]);
  7. if (value != null)
  8. {
  9. return new int[]{i, value};
  10. }
  11. table.put(mun[i], i);
  12. }
  13. return null;
  14. }
  15. 复制代码

2.2.2 树

  • 二叉树某一层有多少个节点

2.2.3 排序

  • 双向链表排序(这个就比较过分了,遇到了就自求多福吧)
  1. public static void quickSort(Node head, Node tail) {
  2. if (head == null || tail == null || head == tail || head.next == tail) {
  3. return;
  4. }
  5. if (head != tail) {
  6. Node mid = getMid(head, tail);
  7. quickSort(head, mid);
  8. quickSort(mid.next, tail);
  9. }
  10. }
  11. public static Node getMid(Node start, Node end) {
  12. int base = start.value;
  13. while (start != end) {
  14. while(start != end && base <= end.value) {
  15. end = end.pre;
  16. }
  17. start.value = end.value;
  18. while(start != end && base >= start.value) {
  19. start = start.next;
  20. }
  21. end.value = start.value;
  22. }
  23. start.value = base;
  24. return start;
  25. } // 使用如内部实现使用双向链表的LinkedList容器实现的快排
  26. public static void quickSort(List<Integer> list) {
  27. if (list == null || list.isEmpty()) {
  28. return;
  29. }
  30. quickSort(list, 0, list.size() - 1);
  31. }
  32. private static void quickSort(List<Integer> list, int i, int j) {
  33. if (i < j) {
  34. int mid = partition(list, i, j);
  35. partition(list, i, mid);
  36. partition(list,mid + 1, j);
  37. }
  38. }
  39. private static int partition(List<Integer> list, int i, int j) {
  40. int baseVal = list.get(i);
  41. while (i < j) {
  42. while (i < j && baseVal <= list.get(j)) {
  43. j--;
  44. }
  45. list.set(i, list.get(j));
  46. while (i < j && baseVal >= list.get(i)) {
  47. i++;
  48. }
  49. list.set(j, list.get(i));
  50. }
  51. list.set(i, baseVal);
  52. return i;
  53. }复制代码


  • 常见排序,如堆排序,快速,归并,冒泡等,还得记住他们的时间复杂度.

2.3项目
2.3.1 视频聊天使用什么协议?
不要答TCP,答RTMP实时传输协议,RTMP在Github也有很多开源实现,建议面这方面的同学可以去了解一下.
2.3.2 你在项目中遇到的一些问题,如何解决,思路是什么?
这一块比较抽象,根据你自己的项目来,着重讲你比较熟悉,有把握的模块,一般面试官都会从中抽取一些问题来向你提问.
3 最后说一些个人认为比较重要的事
3.1 积极准备、不断试错

  • 机会都是留给有准备的人的,千万不要想着不准备上战场就能成功.
  • 多看面经,面经就是面试官们的招聘导向,透过阅读大量的面经,你能够感受得到面试官想要找到什么样的人,并且你可以有针对性地去准备.
  • 不断地去面试,如果你这次面试失败了,那一定要好好总结其中的原因,一定要想方设法地从面试官口中套出自己的不足,这样你下次面试成功的概率就会增加.


免费Java高级资料需要自己领取,涵盖了Java、Redis、MongoDB、MySQL、Zookeeper、Spring Cloud、Dubbo高并发分布式等教程!!!

​传送门:baminute.com:8082/activity.ht…


转载于:https://juejin.im/post/5d2ddfeae51d4510b71da69a

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/秋刀鱼在做梦/article/detail/887920
推荐阅读
相关标签
  

闽ICP备14008679号