Robinhood 电面

Given a stream of incoming "buy" and "sell" orders (as lists of limit price, quantity, and side, like ["155", "3", "buy"]), determine the total quantity (or number of "shares") executed.

  1. A "buy" order can be executed if there is a corresponding "sell" order with a price that is less than or equal to the price of the "buy" order.
  2. Similarly, a "sell" order can be executed if there is a corresponding "buy" order with a price that is greater than or equal to the price of the "sell" order.
  3. It is possible that an order does not execute immediately if it isn't paired to a counterparty. In that case, you should keep track of that order and execute it at a later time when a pairing order is found.

You should ensure that orders are filled immediately at the best possible price. That is, an order should be executed when it is processed, if possible. Further, "buy" orders should execute at the lowest possible price and "sell" orders at the highest possible price at the time the order is handled.

Note that orders can be partially executed.

--- Sample Input ---

orders = [
  ['150', '5', 'buy'],    # Order A
  ['190', '1', 'sell'],   # Order B
  ['200', '1', 'sell'],   # Order C
  ['100', '9', 'buy'],    # Order D
  ['140', '8', 'sell'],   # Order E
  ['210', '4', 'buy'],    # Order F
]

Sample Output
9

思路:这题老实讲,不难,只是题目意思理解费劲,解题关键是用两个priorityqueue,一个用来存buy的最大,一个用来存sell的最小。

然后扫描,update 两个pq;

robinhood的面经: https://docs.google.com/document/d/1VwYu1omqQvV0HLHVenKWu7kg1jStBGBfXyivLXyUzVw/edit

/*
   price  share buy/sell
  ['150', '5', 'buy'],    # Order A
  ['190', '1', 'sell'],   # Order B
  ['200', '1', 'sell'],   # Order C
  ['100', '9', 'buy'],    # Order D
  ['140', '8', 'sell'],   # Order E 5
  ['210', '4', 'buy'],    # Order F 3 + 1

  output: 9;
*/

//100, 140, 150, 
//rule1: buy 
private class Node {
    public int price;
    public int share;
    public Node(int price, int share) {
        this.price = price;
        this.share = share;
    }
}

private class BuyNodeComparator implements Comparator<Node> {
    @Override
    public int compare(Node a, Node b) {
        return b.price - a.price;
    }
}

private class SellNodeComparator implements Comparator<Node> {
    @Override
    public int compare(Node a, Node b) {
        return a.price - b.price;
    }
}

int order_book(String[][] orders) {
    if(orders == null || orders.length == 0 || orders[0].length == 0) {
        return 0;
    }
    int res = 0;
    PriorityQueue<Node> buypq = new PriorityQueue<Node>(new BuyNodeComparator());
    PriorityQueue<Node> sellpq = new PriorityQueue<Node>(new SellNodeComparator());

    for(int i = 0; i < orders.length; i++) {
        int price = Integer.parseInt(orders[i][0]);
        int share = Integer.parseInt(orders[i][1]);

        if(orders[i][2].equals("buy")) {
            Node buynode = new Node(price, share);
            while(!sellpq.isEmpty() && sellpq.peek().price <= buynode.price && buynode.share > 0) {
                Node sellnode = sellpq.poll();
                if(sellnode.share > buynode.share) {
                    res += buynode.share;
                    sellnode.share = sellnode.share - buynode.share;
                    sellpq.offer(sellnode);
                    buynode.share = 0;
                } else {
                    // sellnode.share <= buynode.share;
                    res += sellnode.share;
                    buynode.share -= sellnode.share;
                }
            }
            if(buynode.share > 0) {
                buypq.offer(buynode);
            }
        } else {
            // “SELL” buy price >= sell price;
            Node sellnode = new Node(price, share);
            while(!buypq.isEmpty() && buypq.peek().price >= sellnode.price && sellnode.share > 0) {
                Node buynode = buypq.poll();
                if(buynode.share > sellnode.share) {
                    res += sellnode.share;
                    buynode.share = buynode.share - sellnode.share;
                    buypq.offer(buynode);
                    sellnode.share = 0;
                } else {
                    // buynode.share >= sellnode.share;
                    res += buynode.share;
                    sellnode.share -= buynode.share;
                }
            }
            if(sellnode.share > 0) {
                sellpq.offer(sellnode);
            }
        }
    }
    return res;
}
/*
Code Review: Money Transfer
 
Since our systems process billions of dollars in trades per week, it’s
important that trade processing and money transfer are error-free.
 
Suppose the code below comes to you for code review. The responsibility of
this code block is to transfer money from one account to another
and send emails to both account owners when the transfer is complete.
 
This code is logically correct. However, given that we operate in the real
world on a distributed system with millions of people transferring money in
and out, what could go wrong? How would you change the code below to
accommodate these failure scenarios?
 
Don't focus on code style or refactoring the code.
 
cache;

master slave;

several servers

only 1 DB; 

Oracle;

def bad_transfer(src_account, dst_account, amount):
    lock;
    src_cash = src_account.cash # DB read
    dst_cash = dst_account.cash # DB read
    if src_cash < amount:
        raise InsufficientFunds
    src_account.cash = src_cash - amount # DB write
    src_account.send_src_transfer_email()
    dst_account.cash = dst_cash + amount # DB write
    dst_account.send_dst_transfer_email()
    unlock;
*/

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值