美團面試記錄

一面:

1、開場先進行了一下自我介紹
2、問了問實習公司的項目
3、算法題讓說一下簡單的思路

假設有一套業務系統,服務於最多1000萬用戶。

每個用戶通過唯一的用戶名標識,用戶名可以包含數字、字母、下劃線,不超過16個字符。

用戶需要通過“註冊”的動作,將自己的用戶名註冊到業務系統上,註冊成功後業務系統隨機爲其分配一個[0,31]的數字。

用戶可以註銷自己的用戶名。註銷時,用戶需要輸入自己的用戶名,業務系統刪除保存的用戶名和與之對應的數字。

用戶也可以通過自己的用戶名查找或修改業務系統爲其分配的數字。

(1)請設計一種算法實現上述需求。操作速度優先。方便起見,用戶名以及對應的數字都保存在內存中。

(2)如果用戶名限制爲中國大陸的手機號碼(假設目前共13013120個號段),爲追求上述操作速度最快,請問你有何建議?

4、問了一下 用沒用過js、計算機網絡方面的知識熟不熟
5、說一下hashmap
6、說一下 Redis 持久化

一面被重置

1、MysQl用的什麼數據結構,MySQL的btree索引和hash索引的區別[12]

2、鏈表和數組的區別,爲什麼用數組查找的快,分別在什麼情況下用

3、說一下Hash表[1, 2]

4、鏈表反轉

public static ListNode reverseList(ListNode head) {
        ListNode pre = null;
        ListNode cur = head;
        ListNode last;
        while (cur != null) {
            last = cur.next;
            cur.next = pre;
            pre = cur;
            cur = last;
        }
        return pre;
    }

5、前序遍歷和中序遍歷構建二叉樹

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    /*
       根據 前序遍歷 和 中序遍歷 的特點構建二叉樹
            
            前序遍歷:  [根節點 | 左子樹 | 右子樹]
            中序遍歷:  [左子樹 | 根節點 | 右子樹]

       方法:遞歸

            使用 字典 存儲中序遍歷遍歷中各個節點的值及其索引
            前序遍歷第一個元素一定爲根節點
            根據根節點值與 字典找到 中序遍歷中根節點索引
            因爲 前序遍歷:  [根節點 | 左子樹 | 右子樹] 我們不知道左子樹的結束位置
            所以我們需要 根據 中序遍歷中根節點索引 記錄左子樹節點個數 右子樹節點個數
            左子樹節點個數 = 中序遍歷中根節點索引 - 開始位置
            右子樹節點個數 = 結束位置 - 中序遍歷中根節點索引
    
     */
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        if (preorder == null || preorder.length == 0) {
            return null;
        }
        int len = preorder.length;
        Map<Integer, Integer> treeMap = new HashMap<>();

        for (int i = 0; i < len; i++) {
            treeMap.put(inorder[i], i);
        }
        return buildTree(preorder, 0, len - 1, inorder, 0, len - 1, treeMap);
    }

    public TreeNode buildTree(int[] preorder, int preStart, int preEnd, int[] inorder, int inStart, int inEnd, Map<Integer, Integer> map) {
        if (preStart > preEnd) {
            return null;
        }
        
        int rootIndex = map.get(preorder[preStart]);
        int leftNum = rootIndex - inStart;
        int rightNum = inEnd - rootIndex;
        if (preStart == preEnd) {
            return new TreeNode(preorder[preStart]);
        }

        TreeNode root = new TreeNode(preorder[preStart]);
        root.left = buildTree(preorder, preStart + 1, preStart + leftNum, inorder, inStart, rootIndex - 1, map);
        root.right = buildTree(preorder, preStart + leftNum + 1, preEnd, inorder, rootIndex + 1, inEnd, map);
        return root;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章