回复: 0
跳转到指定楼层
上一主题 下一主题
收起左侧

WhatsApp OA questions and answers

全局:

2016(7-9月) 码农类General 硕士 全职@whatsapp - 校园招聘会 - 其他  | | Fail | 应届毕业生

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
#include <iostream>
using namespace std;
// question 1: create a new datatype for a singly linked list
struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};

class Solution {
public:
    // question 2: write a function that merges two sorted linked lists.
    ListNode *merge(ListNode *runner1, ListNode *runner2) {
        ListNode *pre = new ListNode(0);
        ListNode *runner = pre;
        while (runner1 || runner2) {
            if (!runner2) {
                runner->next = runner1;
                runner1 = runner1->next;
            }
            else if (!runner1) {
                runner->next = runner2;
                runner2 = runner2->next;
            }
            else if (runner1->val < runner2->val) {
                runner->next = runner1;
                runner1 = runner1->next;
            }
            else {
                runner->next = runner2;
                runner2 = runner2->next;
            }
            runner = runner->next;
        }
        return pre->next;
    }

    // question 3: full merge-sort algorithm
    ListNode* mergeSort(ListNode* head) {
        if (!head || !head->next) return head;
        ListNode *slow_runner = head, *fast_runner = head;
        while (fast_runner->next && fast_runner->next->next) {
            slow_runner = slow_runner->next;
            fast_runner = fast_runner->next->next;
        }
        ListNode *head1 = head, *head2 = slow_runner->next;
        slow_runner->next = NULL;
        return merge(mergeSort(head1), mergeSort(head2));
    }

    // to judge if my answer and expected answer is the same
    bool isEqual(ListNode *myAnswer, ListNode *expectedAnswer) {
        if (!myAnswer && !expectedAnswer)
            return true;
        if (!myAnswer || !expectedAnswer)
            return false;
        while (myAnswer && expectedAnswer) {
            if (myAnswer->val != expectedAnswer->val)
                return false;
            myAnswer = myAnswer->next;
            expectedAnswer = expectedAnswer->next;
        }
        if (myAnswer || expectedAnswer)
            return false;
        return true;
    }

    // print out the linked list
    void printList(ListNode *list) {
        if (!list) {
            cout << "NULL" << endl;
            return;
        }
        while (list) {
            cout << list->val << " ";
            list = list->next;
        }
        cout << endl;
    }
};
int main() {
    // question 4: create test cases
    Solution sol;
    ListNode *myAnswer = NULL, *correctAnswer = NULL;

    cout << "Test case 1: " << endl;;
    ListNode *tc1_1 = new ListNode(1); ListNode *tc1_2 = new ListNode(3); ListNode *tc1_3 = new ListNode(2);
    tc1_1->next = tc1_2; tc1_2->next = tc1_3;
    cout << "Input: ";
    sol.printList(tc1_1);
    ListNode *sorted_tc1_1 = new ListNode(1); ListNode *sorted_tc1_2 = new ListNode(2); ListNode *so
您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
->next = tc8_2; tc8_2->next = tc8_3;
    cout << "Input: ";
    sol.printList(tc8_1);
    ListNode *sorted_tc8_1 = new ListNode(1); ListNode *sorted_tc8_2 = new ListNode(2); ListNode *sorted_tc8_3 = new ListNode(3);
    sorted_tc8_1->next = sorted_tc8_2; sorted_tc8_2->next = sorted_tc8_3;
    myAnswer = sol.mergeSort(tc8_1);
    correctAnswer = sorted_tc8_1;
    cout << "My answer: ";
    sol.printList(myAnswer);
    cout << "Expected answer: ";
    sol.printList(correctAnswer);
    if (sol.isEqual(myAnswer, correctAnswer))
        cout << "PASS!" << endl;
    else
        cout << "FAIL!" << endl;
    cout << endl;

    cout << "Test case 9: " << endl;;
    ListNode *tc9_1 = new ListNode(1); ListNode *tc9_2 = new ListNode(1); ListNode *tc9_3 = new ListNode(1);
    tc9_1->next = tc9_2; tc9_2->next = tc9_3;
    cout << "Input: ";
    sol.printList(tc9_1);
    ListNode *sorted_tc9_1 = new ListNode(1); ListNode *sorted_tc9_2 = new ListNode(1); ListNode *sorted_tc9_3 = new ListNode(1);
    sorted_tc9_1->next = sorted_tc9_2; sorted_tc9_2->next = sorted_tc9_3;
    myAnswer = sol.mergeSort(tc9_1);
    correctAnswer = sorted_tc9_1;
    cout << "My answer: ";
    sol.printList(myAnswer);
    cout << "Expected answer: ";
    sol.printList(correctAnswer);
    if (sol.isEqual(myAnswer, correctAnswer))
        cout << "PASS!" << endl;
    else
        cout << "FAIL!" << endl;
    cout << endl;
    return 0;
}



上一篇:WhatsApp Full time interview questions
下一篇:Coursera OA1 9/23
无效楼层,该帖已经被删除
您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表