Hulu日常实习面经 (SDE/RSDE)

Hulu日常实习面经 (SDE/RSDE)

一面

时间:2019年4月17日 地点:校内电话面试 形式:电话里交流问题,网页上写代码 时长:1 hour

先简单问了简历上的项目,面试官是做JavaEE的,简历上写的大多是CV的项目,所以面试官问的不多,就问了7分钟。然后就是写代码:链表的归并排序,要求时间复杂度O(nlogn),空间复杂度O(1). (这是一道LeetCode Medium的题,题目链接:148. Sort List) 先说思路,面试官觉得思路ok开始写代码,是在一个叫nova的共享页面上写的,能看到共享者的光标,面试官的光标会follow你的代码。我用的Java写的,核心代码是mergeSort和merge两个方法,其他是自己写的测试代码:

package firstInterview;

class ListNode {
    int val;
    ListNode next;
    
    public ListNode(int val)
    {
        this.val = val;
    }
}

public class Solution {
    public static ListNode mergeSort(ListNode head)
    {
        if (head == null || head.next == null)
        {
            return head;
        }
        ListNode fast = head.next, slow = head;
        // Compute mid node of list using fast & slow pointer
        // Every time slow pointer goes 1 step while fast pointer goes 2 steps
        // Mid node is stored in slow pointer
        while (fast != null)
        {
            fast = fast.next;
            if (fast != null)
            {
            	fast = fast.next;
            	slow = slow.next;
            }
        }
        ListNode h2 = mergeSort(slow.next);
        slow.next = null;
        ListNode h1 = mergeSort(head);
        return merge(h1, h2);
    }
    
    public static ListNode merge(ListNode h1, ListNode h2)
    {
        ListNode p1 = h1, p2 = h2, newHead = new ListNode(0), h = newHead;
        // newHead: use an auxiliary head for new list
        while (p1 != null && p2 != null)
        {
            if (p1.val < p2.val)
            {
                ListNode tmp = p1;
                p1 = p1.next;
                h.next = tmp;
                h = h.next;
            }
            else
            {
                ListNode tmp = p2;
                p2 = p2.next;
                h.next = tmp;
                h = h.next;
            }
        }
        while (p1 != null)
        {
            ListNode tmp = p1;
            p1 = p1.next;
            h.next = tmp;
            h = h.next;
        }
        while (p2 != null)
        {
            ListNode tmp = p2;
            p2 = p2.next;
            h.next = tmp;
            h = h.next;
        }
        h.next = null;
        return newHead.next;
    }
    
    /**
    * generate list from array for debugging
    * @param arr
    */
    public static ListNode genListFromArray(int[] arr)
    {
        ListNode head = new ListNode(0), ptr = head;
        // head: use auxiliary head for new list
        for (int i: arr)
        {
            ptr.next = new ListNode(i);
        	ptr = ptr.next;
        }
        return head.next;
    }
    
    /**
     * print list for debugging
     * @param head
     */
    public static void printList(ListNode head)
    {
    	ListNode ptr = head;
    	while (ptr != null)
    	{
    		System.out.print(ptr.val + " ");
    		ptr = ptr.next;
    	}
    	System.out.println();
    }
    
    public static void main(String[] args)
    {
        final int[] arr = {4,5,9,1,0,10};
    	ListNode head = genListFromArray(arr);
        ListNode newHead = mergeSort(head);
        printList(newHead);
    }
}

以上代码是我面试复盘时候润色过的,当场写的代码找链表中点用的是两次遍历,在面试官的提醒下想到了快慢指针并修改了代码。


持续更新中~~~

猜你喜欢

转载自blog.csdn.net/da_kao_la/article/details/89386196
今日推荐