阿里現(xiàn)在的面試這么難了嗎?
阿里巴巴是目前全球最大的網(wǎng)上交易市場(chǎng)和商務(wù)交流社區(qū),其地位是不可撼動(dòng)的,沒(méi)點(diǎn)真本事真的不容易進(jìn)里面工作。今天所長(zhǎng)在V2EX里看到有人吐槽阿里現(xiàn)在的面試這么難了嗎?一起看看怎么個(gè)難法。

? //經(jīng)過(guò)我的抽象大致是這樣的,重量和數(shù)量問(wèn)題不用考慮
? ?public static class Product{}
? ?public static class Package{}
? ?//此物品是否在包裹中
? ?public boolean productInPackage(Package packet, Product product) { ***** }
? ?//完成此方法: 每個(gè)背包可以有某些物品任意件,找出最少的背包包含所有的物品
? ?public Map findLeastPackages(List products, List packages) {} 
??public static class Product{}
? ?public static class Package{}
? ?public boolean productInPackage(Package packet, Product product) {}
? ?// n 個(gè)背包, m 個(gè)物品, 每個(gè)背包可以有某些物品任意件, 找出最少的背包包含所有的物品 ?注: 此題一定有解
? ?//不考慮背包的權(quán)重、背包中物品權(quán)重、物品數(shù)量和重量
? ?public MapfindLeastPackages(List products, List packages) {
? ? ? ?if (products == null || products.isEmpty() || packages == null || packages.isEmpty()) {
? ? ? ? ? ?return null;
? ? ? ?}
? ? ? ?SetproductSet = new HashSet<>(products);
? ? ? ?SetpackageSet = new HashSet<>(packages);
? ? ? ?int productsSize = productSet.size();
? ? ? ?int packagesSize = packageSet.size();
? ? ? ?//返回值
? ? ? ?MappriorityPackages = new HashMap<>(productsSize);
? ? ? ?//包裹 <=> 包裹里物品的雙向?qū)?yīng)
? ? ? ?//可以使用 Guava 的 Bimap
? ? ? ?Map> allPackages = new HashMap<>(packagesSize);
? ? ? ?Map, Package> productSetPackage = new HashMap<>(packagesSize);
? ? ? ?//尋找到包含數(shù)量物品種類(lèi)最大的包裹
? ? ? ?Package maxProductsPackage = null;
? ? ? ?SetproductTempSet = null;
? ? ? ?for (Package aPackage : packageSet) {
? ? ? ? ? ?if (aPackage == null) {
? ? ? ? ? ? ? ?continue;
? ? ? ? ? ?}
? ? ? ? ? ?//初始化 aPackage
? ? ? ? ? ?allPackages.put(aPackage, (productTempSet = new HashSet<>()));
? ? ? ? ? ?productSetPackage.put(productTempSet, aPackage);
? ? ? ? ? ?for (Product product : productSet) {
? ? ? ? ? ? ? ?if (product == null) {
? ? ? ? ? ? ? ? ? ?continue;
? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?//物品在背包中, 放入背包
? ? ? ? ? ? ? ?if (productInPackage(aPackage, product)) {
? ? ? ? ? ? ? ? ? ?allPackages.get(aPackage).add(product);
? ? ? ? ? ? ? ?}
? ? ? ? ? ?}
? ? ? ? ? ?if (maxProductsPackage == null) {
? ? ? ? ? ? ? ?maxProductsPackage = aPackage;
? ? ? ? ? ?} else {
? ? ? ? ? ? ? ?maxProductsPackage = allPackages.get(aPackage).size() >= allPackages.get(maxProductsPackage).size() ? aPackage : maxProductsPackage;
? ? ? ? ? ?}
? ? ? ?}
? ? ? ?//已經(jīng)找到背包有哪些物品
? ? ? ?//開(kāi)始集合運(yùn)算
? ? ? ?//maxProductsPackage 種類(lèi)最多, 說(shuō)明這個(gè)一定是最優(yōu)里面的
? ? ? ?//maxProductsPackage 包含所有種類(lèi) 直接返回
? ? ? ?if (allPackages.get(maxProductsPackage).size() == productSet.size()) {
? ? ? ? ? ?for (Product product : productSet) {
? ? ? ? ? ? ? ?priorityPackages.put(product, maxProductsPackage);
? ? ? ? ? ?}
? ? ? ? ? ?return priorityPackages;
? ? ? ?}
? ? ? ?//todo 機(jī)試的就寫(xiě)道這里 ?主要記不太清楚集合的交叉并補(bǔ) API,時(shí)間也不足 ?(40 分鐘白板寫(xiě)代碼)
? ? ? ?//沒(méi)有使用 lambda 、Stream API 主要是記憶問(wèn)題(白板寫(xiě)代碼), 還有通過(guò)數(shù)組包裝局部變量, 還有多層循環(huán) break
? ? ? ?// 1.刪除 maxProductsPackage 子集的包裹
? ? ? ?// 2.找到其他包裹和 maxProductsPackage 差值最大的包裹, 并取并集作為新的 maxProductsPackage
? ? ? ?// 3.判斷 maxProductsPackage 是否包含所有物品, 是的話(huà)返回, 不是的話(huà)重復(fù)第一步直到找到結(jié)果或集合為空(一定有答案)
? ? ? ?SetmaxProducts = allPackages.get(maxProductsPackage);
? ? ? ?SetsecondMaxProducts = null;
? ? ? ?//刪除最大包裹
? ? ? ?allPackages.remove(maxProductsPackage);
? ? ? ?//留下來(lái)的包裹 [不在最大包裹之內(nèi), 有差值, 但不是差值最大的] ?找到差值最大的合并到 maxProducts, 然后轉(zhuǎn)移到 mergeSets
? ? ? ?HashSet> remainSets = new HashSet<>(allPackages.values());
? ? ? ?//和最大包裹差值最大的, 已經(jīng)合并到最大包裹內(nèi) [結(jié)果一定在這個(gè)里面]
? ? ? ?Set> mergeSets = new HashSet<>(packagesSize);
? ? ? ?mergeSets.add(maxProducts);
? ? ? ?while (maxProducts.size() != productsSize) {
? ? ? ? ? ?//如果 remainSets 為空,且 maxProducts.size() != productsSize 說(shuō)明沒(méi)有答案[不會(huì)發(fā)生]
? ? ? ? ? ?//可以把所有包裹相加去重后如果!= productsSize, 說(shuō)明沒(méi)有答案, 這樣可以更快排除,這里只是以防萬(wàn)一
? ? ? ? ? ?if (remainSets.isEmpty()) {
? ? ? ? ? ? ? ?return null;
? ? ? ? ? ?}
? ? ? ? ? ?//尋找次大的包裹, 不需要比較優(yōu)先級(jí) [使用 iterator 模式刪除元素, 優(yōu)化循環(huán)]
? ? ? ? ? ?Iterator> iterator = remainSets.iterator();
? ? ? ? ? ?while (iterator.hasNext()) {
? ? ? ? ? ? ? ?Setnext = iterator.next();
? ? ? ? ? ? ? ?next.removeAll(maxProducts);
? ? ? ? ? ? ? ?//是 maxProducts 的子集
? ? ? ? ? ? ? ?if (next.isEmpty()) {
? ? ? ? ? ? ? ? ? ?iterator.remove();
? ? ? ? ? ? ? ? ? ?continue;
? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?//初始化 secondMaxProducts ? ?可以刪除次大元素減小集合
? ? ? ? ? ? ? ?if (secondMaxProducts == null) {
? ? ? ? ? ? ? ? ? ?secondMaxProducts = next;
? ? ? ? ? ? ? ?} else {
? ? ? ? ? ? ? ? ? ?secondMaxProducts = next.size() > secondMaxProducts.size() ? next : secondMaxProducts;
? ? ? ? ? ? ? ?}
? ? ? ? ? ?}
? ? ? ? ? ?// 已經(jīng)找完,退出循環(huán)
? ? ? ? ? ?if (secondMaxProducts == null || secondMaxProducts.size() == 0) {
? ? ? ? ? ? ? ?break;
? ? ? ? ? ?}
? ? ? ? ? ?// 把 secondMaxProducts 加入到 maxProducts
? ? ? ? ? ?////更新 maxProducts
? ? ? ? ? ?maxProducts.addAll(secondMaxProducts);
? ? ? ? ? ?//更新 mergeSets
? ? ? ? ? ?mergeSets.add(secondMaxProducts);
? ? ? ? ? ?//刪除此元素
? ? ? ? ? ?remainSets.remove(secondMaxProducts);
? ? ? ? ? ?secondMaxProducts = null;
? ? ? ?}
? ? ? ?//mergeSets 即為所求
? ? ? ?mergeSets.forEach(set -> set.forEach(product -> priorityPackages.put(product, productSetPackage.get(set))));
? ? ? ?return priorityPackages;
? ?}
面試難不難,因人而異,因職位而異,因部門(mén)而異。想通過(guò)面試,能力當(dāng)然不用說(shuō),綜合素質(zhì)也很重要,運(yùn)氣也占了一部分。

最后奉上一位大佬對(duì)面試的看法。

評(píng)論
圖片
表情
