📣 黑五价提前开抢|VIP一年通行证5折,蓝莓、Offer多多同步特惠
回复: 0
跳转到指定楼层
上一主题 下一主题
收起左侧

准备amazon OA2自己测试过的一些代码

全局:

2017(1-3月) 码农类General 硕士 全职@amazon - 校园招聘会 - 在线笔试  | | Other | 应届毕业生

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

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

x
前两周完成了Amazon OA2,coding 部分抽到的水题,直接分配群面了(也可能是WS没做好)。分享一下 准备OA期间,自己写的 热门OA C++代码(在C98下 编译测试过)
文档第一部分代码,在MST之前的代码是 oder dependency

#include <stdio.h>
#include<iostream> //* for string, vector, and so on
#include<string> //* for string
#include<queue>//* priority_queue
#include<algorithm>// sort
#include<functional>//* greater
#include<vector>
#include<map>

using namespace std;

//* order dependency
struct gnode{
    int id, degree;
    vector<int> adj_ids;
};

vector<gnode> make_graph(vector<pair<int, int> >& preq, int ncs){
    vector<gnode> nodes;
    for(int i = 0; i< ncs; i++){
        gnode nd; nd.id = i; nd.degree = 0;
        nodes.push_back(nd);
    }
    for(int i =0; i< preq.size(); i++){
        int cid = preq[i].second; int adj = preq[i].first;
        nodes[adj].degree++; //* note!
        nodes[cid].adj_ids.push_back(adj);
    }
    return nodes;
}

void helper(vector<int>& ret, int ncs, vector<gnode>& nodes ){
    if(ret.size() == ncs) return;
    int judge = 0;
    for(int i = 0; i < nodes.size(); i++){
        gnode& nd  = nodes[i];
        if(nd.degree == 0){
            judge = 1;
            ret.push_back(nd.id);
            nd.degree = -1;
            for(int j = 0; j < nd.adj_ids.size(); j++) nodes[nd.adj_ids[j]].degree--;
        }
    }
    if(not judge) return;
    helper(ret, ncs, nodes);
   
}
vector<int> findOrder(int numCourses, vector<pair<int, int> >& prerequisites) {
    vector<int> ret, empty;
    vector<gnode> nodes = make_graph(prerequisites, numCourses);
    helper(ret, numCourses, nodes);
    if(ret.size() != numCourses) return empty;
    return ret;
   
}

//* MST
class MST{
public:
    struct connection{
        string node1, node2;
        int cost;
    };
    struct node{
        string city;
        map<string, int> adj_cost;
    };
    void make_graph(map<string,node>&graph, vector<connection>::iterator conit, vector<connection>::iterator endit){
        if(conit == endit) return;
        string city1 = conit->node1, city2 = conit->node2;
        int cost = conit->cost;
        graph[city1].city = city1; graph[city1].adj_cost[city2] = cost;
        graph[city2].city = city2; graph[city2].adj_cost[city1] = cost; //* important: to include two city, oherwise graph.size() will no
您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Popular Interview Questions from Top Companies
eft">    struct RandomListNode {
             int label;
             RandomListNode *next, *random;
             RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
         };
    RandomListNode *copyRandomList(RandomListNode *head) {
        if(not head) return NULL;
        map<RandomListNode*, RandomListNode*> hash; //* the first is to record the orig, the second is the p2 new node
        RandomListNode* p1 = head;
        RandomListNode* p2 = new RandomListNode(p1->label);
        hash[head] = p2;//* can be also used for return;
        while(p1){
            if(p1->next){
                if(hash[p1->next]) p2->next = hash[p1->next]; //* note not p1->next, because this was just created new node
                else{
                    p2->next = new RandomListNode(p1->next->label);
                    hash[p1->next] = p2->next;
                }
            }
            if(p1->random){
                if(hash[p1->random]) p2->random = hash[p1->random];
                else{
                    p2->random = new RandomListNode(p1->random->label);
                    hash[p1->random] = p2->random;
                }
            }
            p1 = p1->next;
            p2 = p2->next;
        }
        return hash[head];
    }
};


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

本版积分规则

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