20190807 BlackRock技术面试一面 SDE实习 远程 不限时间

如无特殊说明,必须使用Java进行解答。

本人给出的答案是自己的回答,并不是最优解。欢迎博友讨论,并指出可以优化之处。

1. 倒序排列给定字符串中的字母和数字(省略其他),用“-”连接

2. 使用最少纸币/硬币找零问题 要求展现OOP (是规则的面额,所以直接贪心算法)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;/*** Decide if a certain note/coin is needed for change.*//*** all the coins and notes*/
enum Money {Fifty_Pound(50, "Fifty Pounds"), Twenty_Pound(20, "Twenty Pounds"), Ten_Pound(10,"Ten Pounds"), Five_Pound(5, "Five Pounds"), Two_Pound(2,"Two Pounds"), One_Pound(1, "One Pound"), Fifty_Pence(0.5, "Fifty Pence"), Twenty_Pence(0.2, "Twenty Pence"), Ten_Pence(0.1, "Ten Pence"),Five_Pence(0.05, "Five Pence"), Two_Pence(0.02, "Two Pence"), One_Pence( 0.01, "One Pence");private double value;private String code;private Money(double value, String code) {this.value = value;this.code = code;}public double getValue() {return value;}public String getCode() {return code;}}public class test {/*** Iterate through each line of input.*/public static void main(String[] args) throws IOException {InputStreamReader reader = new InputStreamReader(System.in);BufferedReader in = new BufferedReader(reader);try {double purchasePrice = Double.parseDouble(in.readLine());double cash = Double.parseDouble(in.readLine());test.calculateChange(purchasePrice, cash);} catch (Exception e) {System.out.println(e);}}public static void calculateChange(double purchasePrice, double cash) {// Access your code here. Feel free to create other classes as requiredif (cash == purchasePrice) { // no change neededSystem.out.println("ZERO");return;}// not enough cash or negative numberif (cash < purchasePrice || cash < 0 || purchasePrice < 0) {System.out.println("ERROR");return;}double change = cash - purchasePrice; // amount that should be given back to customersString output = ""; // output on the screenMoney[] values = Money.values();for (Money money : values) {int amount = (int) (change / money.getValue());change -= amount * money.getValue();// solve accuracy problem of java compilerchange = (double) (Math.round(change * 100)) / 100;if (amount != 0) {for (int i = 0; i < amount; i++) {output += money.getCode() + ", ";}}if (change == 0) { // no need to search smaller denominationsbreak;}}if (output != "" || output != ", ") {output = output.substring(0, output.length() - 2); // get rid of last ", "}System.out.println(output);}
}

3.

编程挑战说明:可选Java,Python

Given a pattern as the first argument and a string of blobs split by | show the number of times the pattern is present in each blob and the total number of matches.

输入:

The input consists of the pattern ("bc" in the example) which is separated by a semicolon followed by a list of blobs ("bcdefbcbebc|abcdebcfgsdf|cbdbesfbcy|1bcdef23423bc32" in the example). Example input: bc;bcdefbcbebc|abcdebcfgsdf|cbdbesfbcy|1bcdef23423bc32

输出:

The output should consist of the number of occurrences of the pattern per blob (separated by |). Additionally, the final entry should be the summation of all the occurrences (also separated by |).

Example output: 3|2|1|2|8 where 'bc' was repeated 3 times, 2 times, 1 time, 2 times in the 4 blobs passed in. And 8 is the summation of all the occurrences (3+2+1+2 = 8)

4. 编程挑战说明:

We say a portfolio matches the benchmark when the number of shares of each asset in the portfolio matches the number of shares of each asset in the benchmark. Your question is to write a program that determines the transactions necessary to make a portfolio match a benchmark.

A portfolio is a collection of assets such as stocks and bonds. A portfolio could have 10 shares of Vodafone stock, 15 shares of Google stock and 15 shares of Microsoft bonds. A benchmark is also just a collection of assets. A benchmark could have 15 shares of Vodafone stock, 10 shares of Google stock and 15 shares of Microsoft bonds.

A transaction is when you “buy” or “sell” a particular asset of certain asset type (“stock” or “bond”). For instance, you can decide to buy 5 shares of Vodafone stock which, given the portfolio described above, would result in you having 15 shares of Vodafone stock. Correspondingly, you decide to sell 5 shares of Microsoft bonds, which would result in 10 shares of Microsoft bonds in the above portfolio.

Assumptions:

Shares are positive decimals

There will always be at least 1 asset present in the Portfolio and Benchmark

A particular asset can be bond, stock, or both. For example, 5 shares of Microsoft bonds and 10 shares of Microsoft stock can both be present in the portfolio/benchmark

The trades should be sorted in alphabetical order based on the names of the assets; if both bonds and stock are present for an asset, list bonds first

输入:

The first part of the input is the Portfolio holdings (in the format Name,AssetType,Shares where each asset is separated by '|' symbol)

The second part of the input is the Benchmark holdings (in the format Name,AssetType,Shares where each asset is separated by '|' symbol)

Example input: Vodafone,STOCK,10|Google,STOCK,15|Microsoft,BOND,15:Vodafone,STOCK,15|Google,STOCK,10|Microsoft,BOND,15

Note that the two parts are separated by the ':' symbol.

输出:

The output is a list of transactions (separated by new line) in the format TransactionType,Name,AssetType,Shares. Note that the TransactionType should only be BUY or SELL.

Example output: SELL,Google,STOCK,5 BUY,Vodafone,STOCK,5

测试案例

测试1

测试输入

Vodafone,STOCK,10|Google,STOCK,15|Microsoft,BOND,15:Vodafone,STOCK,15|Google,STOCK,10|Microsoft,BOND,15

预期输出

SELL,Google,STOCK,5

BUY,Vodafone,STOCK,5

测试2

测试输入

Vodafone,STOCK,10|Google,STOCK,15:Vodafone,STOCK,15|Vodafone,BOND,10|Google,STOCK,10

预期输出

SELL,Google,STOCK,5

BUY,Vodafone,BOND,10

BUY,Vodafone,STOCK,5

测试3

测试输入

Amazon,STOCK,10|Kugo,BOND,100:

测试4

测试输入

:Kugo, STOCK, 50

测试5

测试输入

Amazon,STOCK,10: Amazon,STOCK,10

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;class Asset implements Comparable<Asset> {String name;String type;int share;  // assume positive integer, can change to BigInt to address potential overflowpublic Asset() {}public Asset(String name, String type, int share) {this.name = name;this.type = type;this.share = share;}void setName(String name) {this.name = name;}void setType(String type) {this.type = type;}void setShare(int share) {this.share = share;}String getName() {return name;}String getType() {return type;}int getShare() {return share;}@Overridepublic String toString() {return "name:" + name + " type:" + type + " share:" + share;}/*** compare the name of the asset*/@Overridepublic int compareTo(Asset other) {return this.getName().compareTo(other.getName());}
}public class Main {static ArrayList<Asset> portStock = new ArrayList<>(); // stock in the portfoliostatic ArrayList<Asset> portBond = new ArrayList<>(); // bond in the portfoliostatic ArrayList<Asset> benchStock = new ArrayList<>(); // stock in the benchmarkstatic ArrayList<Asset> benchBond = new ArrayList<>(); // bond in the benchmarkstatic ArrayList<String> sellList = new ArrayList<>();static ArrayList<String> buyList = new ArrayList<>();/*** Iterate through each line of input.*/public static void main(String[] args) throws IOException {InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);BufferedReader in = new BufferedReader(reader);String line;while ((line = in.readLine()) != null) {Main.matchBenchmark(line);updateList();}}public static void updateList() {sellList = new ArrayList<>();buyList = new ArrayList<>();portStock = new ArrayList<>();portBond = new ArrayList<>();benchStock = new ArrayList<>();benchBond = new ArrayList<>();}/*** Check if the asset belongs to bond or stock* * @param source* @param bond* @param stock*/public static void checkAsset(String[] source, ArrayList<Asset> bond, ArrayList<Asset> stock) {for (String s : source) {String[] tmp = s.split(",");int share = Integer.parseInt(tmp[2].trim());Asset asset = new Asset(tmp[0].trim(), tmp[1].trim(), share);if (isBond(tmp[1])) {bond.add(asset);} else if (isStock(tmp[1])) {stock.add(asset);} else {throw new IllegalArgumentException();}}}/*** split the input String into portfolio and benchmark part* * @param input*/public static void splitPortBench(String input) {String[] portAndBench = input.split(":");String[] portfolio;String[] bench;if (!input.contains(":")) { // invalid inputreturn;}// normal case where both portfolio and benchmark have assetselse if (portAndBench.length == 2 && input.substring(0, input.indexOf(":")).contains(",")) {portfolio = portAndBench[0].split("\\|");bench = portAndBench[1].split("\\|");} else if (input.substring(0, input.indexOf(":")).contains(",")) { // no assets in benchmarkportfolio = portAndBench[0].split("\\|");bench = new String[0];} else { // no assets in portfolioportfolio = new String[0];bench = portAndBench[1].split("\\|");}checkAsset(portfolio, portBond, portStock);checkAsset(bench, benchBond, benchStock);}/*** Compare assets in the portfolio and benchmark to help decide if we should buy more or sell* * @param port* @param bench*/public static void comparePortBench(ArrayList<Asset> port, ArrayList<Asset> bench) {int portIndex = 0;int benchIndex = 0;// compare assets in portfolio and benchmarkwhile (portIndex < port.size() || benchIndex < bench.size()) {if (portIndex >= port.size()) { // remaining assets in benchmark not in portfoliofor (int i = benchIndex; i < bench.size(); i++) {Asset tmp = bench.get(i);buyList.add("BUY," + tmp.getName() + "," + tmp.getType() + ","+ tmp.getShare());}break;}if (benchIndex >= bench.size()) { // remaining assets in portfolio not in benchmarkfor (int i = portIndex; i < port.size(); i++) {Asset tmp = port.get(i);sellList.add("SELL," + tmp.getName() + "," + tmp.getType() + ","+ tmp.getShare());}break;}Asset portAsset = port.get(portIndex);Asset benchAsset = bench.get(benchIndex);// compare the number of share of assets in both portfolio and benchmarkif (portAsset.getName().compareTo(benchAsset.getName()) == 0) {if (portAsset.getShare() < benchAsset.getShare()) { // less than expectedbuyList.add("BUY," + portAsset.getName() + "," + portAsset.getType() + ","+ (benchAsset.getShare() - portAsset.getShare()));} else if (portAsset.getShare() > benchAsset.getShare()) { // more than expectedsellList.add("SELL," + portAsset.getName() + "," + portAsset.getType() + ","+ (portAsset.getShare() - benchAsset.getShare()));}portIndex++;benchIndex++;}// this asset is in portfolio, but not in benchmarkelse if (portAsset.getName().compareTo(benchAsset.getName()) < 0) {sellList.add("SELL," + portAsset.getName() + "," + portAsset.getType() + ","+ portAsset.getShare());portIndex++;}// the asset in benchmark is not in portfolioelse if (portAsset.getName().compareTo(benchAsset.getName()) > 0) {buyList.add("BUY," + benchAsset.getName() + "," + benchAsset.getType() + ","+ benchAsset.getShare());benchIndex++;}}}public static void matchBenchmark(String input) {splitPortBench(input);Collections.sort(portBond);Collections.sort(portStock);Collections.sort(benchBond);Collections.sort(benchStock);comparePortBench(portBond, benchBond);comparePortBench(portStock, benchStock);if (sellList.size() > 0) {for (int j = 0; j < sellList.size(); j++)System.out.println(sellList.get(j));}if (buyList.size() > 0) {for (int j = 0; j < buyList.size(); j++)System.out.println(buyList.get(j));}}/*** check if this asset is a stock* * @param input* @return*/public static boolean isStock(String input) {if (input.toLowerCase().contains("stock"))return true;return false;}/*** check if this asset is a bond* * @param input* @return*/public static boolean isBond(String input) {if (input.toLowerCase().contains("bond"))return true;return false;}
}

软件工程师技术面试一面真题相关推荐

  1. 数字IC设计工程师笔试面试经典100题-有答案

    转自知乎答主ictown_数字IC设计工程师笔试面试经典100题-有答案-陈恩 1:什么是同步逻辑和异步逻辑?(汉王) 同步逻辑是时钟之间有固定的因果关系.异步逻辑是各时钟之间没有固定的因果关系. 同 ...

  2. 2019重大信息安全事件_2019上半年信息安全工程师下午案例分析真题与答案解析...

    信管网将在2019年上半年信息安全工程师考试结束后免费发布2019上半年信息安全工程师下午案例分析真题与答案解析供各位考友查看和估分,敬请关注信管网. 2019年信息安全工程师案例分析真题与答案试题一 ...

  3. 如何准备软件工程师的面试

    (作者简介: 王忻,Google 工程师.北京出生,五岁时跟随父母移居美国.中学期间跳了三级,十五岁进入了加州理工大学,加入 Google 前曾在微软等公司工作.) 六月份的时候,我曾经在黑板报上介绍 ...

  4. Google前工程经理王忻:如何准备软件工程师的面试

    http://t.jobdu.com/thread-368-1-1.html 导读:原文作者王忻,Google前工程经理,2003年月加入Google,是Google Lively背后的主导力量,是G ...

  5. 如何准备软件工程师的面试[转载]

    (作者简介: 王忻,Google 工程师.北京出生,五岁时跟随父母移居美国.中学期间跳了三级,十五岁进入了加州理工大学,加入 Google 前曾在微软等公司工作.) 六月份的时候,我曾经在黑板报上介绍 ...

  6. MIS软件工程师的面试问题与方法

    MIS软件工程师的面试问题与方法 2007年09月21日 星期五 11:17 A.M. 一. 软件工程的一般知识 (1) 说说您所知道的软件开发的有关国家标准或国际标准. (2) 一般软件项目要做哪些 ...

  7. 2022年下半年网络工程师上午综合知识真题答案解析

    2022年下半年网络工程师上午综合知识真题答案解析 1.下列存储介质中,读写速度最快的是(). A.光盘 B.硬盘 C.内存 D.Cache [参考答案]D 2.使用DMA不可以实现数据(). A.从 ...

  8. crt显存试题计算机,2008年9月全国计算机三级考试《PC技术》笔试真题

    2008年9月全国计算机三级考试<PC技术>笔试真题 50.下面是有关PC机鼠标器工作原理的叙述: Ⅰ. 鼠标移动时,其运动的距离和方向被变换成二进制信息输入到计算机中 Ⅱ. 鼠标移动信息 ...

  9. 2021年上半年系统集成项目管理工程师下午案例分析真题及答案解析

    2021年上半年系统集成项目管理工程师下午案例分析真题及答案解析 试题一(18分) 某银行计划开发一套信息系统,为了保证交付质量,银行指派小张作为项目的质量保证工程师.项目开始后,小张开始对该项目质量 ...

最新文章

  1. 报名 | 2019年第六届清华大学大数据社会科学讲习班
  2. python补齐空格_如何提高使用Python填补时间序列和数据列表中空白的性能
  3. python turtle画彩虹-Python利用turtle库绘制彩虹代码示例_天津SEO
  4. lodash 源码解读 _.concat()
  5. 一种更清晰的Android架构
  6. UA MATH524 复变函数13 奇点与留数
  7. java convexhull_图像上划凸多边形(convexHull()函数)
  8. 2---多线程文件读写
  9. java xsd 解析 xml文件_Java针对XSD文件验证XML文件的最佳方法是什么?
  10. 无监督学习 | DBSCAN 原理及Sklearn实现
  11. oracle用exp定时备份数据库,使用exp和imp对Oracle数据库进行简单的备份
  12. 对应版本_NET Framework 和对应的 Windows 版本
  13. 由一道题目看抽象向量组的线性相关问题
  14. Hadoop平台简述
  15. python预定义_[Python] Pycharm 预定义 coding 模板
  16. 什么是TCP/IP协议
  17. 从游戏中学习产品设计04:成就篇
  18. 公务员计算机职称有哪些,2017职称考试有哪些
  19. 程序员常会用到的几款软件
  20. win10 premiere cc 软件无声音解决办法

热门文章

  1. Android硬件抽象层(HAL)深入剖析(一)
  2. innodb表 手工导入导出
  3. Asp.net中的AJAX学习记录之一 AJAX技术的崛起与Asp.net AJAX的诞生
  4. 进阶之初探nodeJS
  5. StartActivityForResult(中规中矩版 获得Acivity2的性别选择)
  6. 软考程序员2009年下午试题是否有误?
  7. 《统一沟通-微软-技巧》-14-Exchange 2010 With SP1 OWA Integration
  8. 降Mail十八章(上)
  9. 再议.Net中null的使用规范
  10. ORACLE如何使用DBMS_METADATA.GET_DDL获取DDL语句