【百度面经】提前批Java一面|0721

alt alt alt alt alt alt alt alt alt alt alt alt alt alt alt alt alt

import java.io.*;
import java.util.*;
import java.util.Map.Entry;

public class Top10IPs {

    public static void main(String[] args) throws IOException {
        String inputFilePath = "path/to/large/file.txt";
        String tempDirectory = "path/to/temp/directory/";

        // Step 1: Split the large file into smaller chunks
        List<String> chunkFiles = splitFile(inputFilePath, tempDirectory);

        // Step 2: Count IPs in each chunk and save intermediate results
        List<String> resultFiles = new ArrayList<>();
        for (String chunkFile : chunkFiles) {
            String resultFile = countIPsInChunk(chunkFile, tempDirectory);
            resultFiles.add(resultFile);
        }

        // Step 3: Merge intermediate results and find top 10 IPs
        List<Entry<String, Integer>> top10IPs = mergeResults(resultFiles);
        
        // Print the top 10 IPs
        for (Entry<String, Integer> entry : top10IPs) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }
    }

    // Method to split the large file into smaller chunks
    public static List<String> splitFile(String inputFilePath, String tempDirectory) throws IOException {
        List<String> chunkFiles = new ArrayList<>();
        int chunkSize = 1000000; // Adjust the chunk size as needed
        int chunkIndex = 0;
        
        BufferedReader reader = new BufferedReader(new FileReader(inputFilePath));
        String line;
        while ((line = reader.readLine()) != null) {
            String chunkFilePath = tempDirectory + "chunk_" + chunkIndex + ".txt";
            PrintWriter writer = new PrintWriter(new FileWriter(chunkFilePath, true));
            int lineCount = 0;
            while (lineCount < chunkSize && line != null) {
                writer.println(line);
                line = reader.readLine();
                lineCount++;
            }
            writer.close();
            chunkFiles.add(chunkFilePath);
            chunkIndex++;
        }
        reader.close();
        return chunkFiles;
    }

    // Method to count IPs in each chunk and save intermediate results
    public static String countIPsInChunk(String chunkFilePath, String tempDirectory) throws IOException {
        Map<String, Integer> ipCountMap = new HashMap<>();
        
        BufferedReader reader = new BufferedReader(new FileReader(chunkFilePath));
        String line;
        while ((line = reader.readLine()) != null) {
            ipCountMap.put(line, ipCountMap.getOrDefault(line, 0) + 1);
        }
        reader.close();

        String resultFilePath = tempDirectory + "result_" + chunkFilePath.substring(chunkFilePath.lastIndexOf('_') + 1);
        PrintWriter writer = new PrintWriter(new FileWriter(resultFilePath));
        for (Entry<String, Integer> entry : ipCountMap.entrySet()) {
            writer.println(entry.getKey() + "," + entry.getValue());
        }
        writer.close();
        return resultFilePath;
    }

    // Method to merge intermediate results and find top 10 IPs
    public static List<Entry<String, Integer>> mergeResults(List<String> resultFiles) throws IOException {
        Map<String, Integer> ipCountMap = new HashMap<>();
        
        for (String resultFile : resultFiles) {
            BufferedReader reader = new BufferedReader(new FileReader(resultFile));
            String line;
            while ((line = reader.readLine()) != null) {
                String[] parts = line.split(",");
                String ip = parts[0];
                int count = Integer.parseInt(parts[1]);
                ipCountMap.put(ip, ipCountMap.getOrDefault(ip, 0) + count);
            }
            reader.close();
        }

        // Find the top 10 IPs
        PriorityQueue<Entry<String, Integer>> minHeap = new PriorityQueue<>(Map.Entry.comparingByValue());
        for (Entry<String, Integer> entry : ipCountMap.entrySet()) {
            minHeap.offer(entry);
            if (minHeap.size() > 10) {
                minHeap.poll();
            }
        }
        
        List<Entry<String, Integer>> top10IPs = new ArrayList<>(minHeap);
        top10IPs.sort((e1, e2) -> Integer.compare(e2.getValue(), e1.getValue()));
        return top10IPs;
    }
}

代码说明

  1. splitFile方法:将大文件分割成多个较小的文件,每个文件包含一定数量的IP地址。
  2. countIPsInChunk方法:统计每个小文件中的IP访问次数,并将结果保存到一个中间结果文件中。
  3. mergeResults方法:合并所有中间结果文件,并找出访问次数排名前十的IP地址。

这段代码假设已经根据具体需求调整了块大小和文件路径。此方法有效地处理了大文件,并找出了访问次数最多的前十个IP地址。

注意事项

  • 内存管理:在处理过程中要密切注意内存的使用情况,以避免内存溢出。
  • 磁盘I/O:优化磁盘I/O操作可以显著提高处理效率。
  • 数据一致性:在处理大文件时,要确保数据的完整性和一致性。
  • 错误处理:添加适当的错误处理逻辑以应对文件读取、写入或排序过程中可能出现的异常情况。

通过上述方法,我们可以在不耗尽机器内存的情况下,有效地处理大文件并找出访问次数排名前十的IP地址。

15. 算法题:在长度为N的有序数组中快速查找所有值为M的元素下标(M可能重复出现)

要在长度为N的有序数组中快速查找所有值为M的元素下标,可以使用二分查找来找到值为M的第一个和最后一个位置,然后再遍历这些位置之间的元素获取所有的下标。这种方法的时间复杂度是O(log N) + O(k),其中k是值为M的元素的数量。

下面是一个Java实现:

import java.util.ArrayList;
import java.util.List;

public class FindIndices {

    public static void main(String[] args) {
        int[] nums = {1, 2, 2, 2, 3, 4, 5};
        int target = 2;
        List<Integer> indices = findAllIndices(nums, target);
        System.out.println(indices);  // 输出:[1, 2, 3]
    }

    public static List<Integer> findAllIndices(int[] nums, int target) {
        List<Integer> indices = new ArrayList<>();
        
        // 辅助方法,找到target的第一个和最后一个位置
        int firstIndex = findFirst(nums, target);
        int lastIndex = findLast(nums, target);

        // 如果找到的第一个位置是-1,说明数组中没有target
        if (firstIndex == -1) {
            return indices;
        }

        // 遍历从firstIndex到lastIndex的范围,添加所有位置到结果列表中
        for (int i = firstIndex; i <= lastIndex; i++) {
            indices.add(i);
        }

        return indices;
    }

    // 辅助方法,找到target的第一个位置
    private static int findFirst(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;
        int result = -1;

        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] == target) {
                result = mid;
                right = mid - 1;  // 继续在左边搜索
            } else if (nums[mid] < target) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }

        return result;
    }

    // 辅助方法,找到target的最后一个位置
    private static int findLast(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;
        int result = -1;

        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] == target) {
                result = mid;
                left = mid + 1;  // 继续在右边搜索
            } else if (nums[mid] < target) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }

        return result;
    }
}

代码说明

  1. findAllIndices方法用于找到所有值为target的元素的下标。
  2. findFirst方法用于找到target在数组中的第一个位置。
  3. findLast方法用于找到target在数组中的最后一个位置。
  4. 如果找到的第一个位置是-1,说明数组中没有target,直接返回空列表。
  5. 否则,遍历从firstIndexlastIndex的范围,将所有的下标添加到结果列表中。

面经原帖有三毛六站神发布,答案由程序员Hasity整理。

alt

#软件开发笔面经#
校招面经大全 文章被收录于专栏

收录各个网友分享的各个公司的面经,并给出答案。

全部评论
杜绝白嫖,文章末尾送朵小花花吧。更好的阅读体验:https://app6721.acapp.acwing.com.cn/interview/27
4 回复 分享
发布于 2024-07-23 11:23 北京
大佬啊
点赞 回复 分享
发布于 2024-07-26 10:47 北京
大佬太强了
点赞 回复 分享
发布于 2024-07-24 19:32 香港
点赞 回复 分享
发布于 2024-07-23 19:11 四川
太强了佬!
点赞 回复 分享
发布于 2024-07-22 16:17 北京
点赞 回复 分享
发布于 2024-07-22 15:10 上海

相关推荐

2025-12-25 10:16
已编辑
合肥工业大学 后端工程师
如题,在历经了长达多月的焦急等待,楼主也算是如愿以偿收到了梦中情司的意向了,秋招也终于是落下了帷幕,虽然手中的offer不够打牌,但已经满足了。华为时间线:9.3&nbsp;笔试环节,惊险通过10.15&nbsp;线下面试,前两轮技术面手撕都比较轻松,面试官态度也很好,最后一轮主管面,向主管表达了强烈的意愿,主管很和蔼,面试体验非常棒,1125定律后入池成功11.19&nbsp;收到接口人的保温电话12.9&nbsp;接到部门hr的保温电话,介绍了一下部门负责的工作12.23&nbsp;收到华为的意向书,成为华孝子一枚~期间收到了之前实习过的公司的offer,害怕华子泡不出来就先签三方了,这下不得不毁约了,在此向前司道个歉,也感谢前司对我的认可和托举,祝业务蒸蒸日上~感谢从今年三月开始找暑期实习以来,所有朋友和家人的鼓励,我们宿舍的就业氛围相当好,大家会分享各种有用的信息以及面试中遇到刁钻的面试题,在有人收到offer的时候我们都会发自内心的高兴和祝福,在我去线下面的时候也借我穿过西服.....能在大学四年分入这么好的宿舍拥有这么这么好的舍友除了幸运我找不出其他的形容词。还要感谢我的父母,在我每一次面试前都给予鼓励,也在失败的时候安慰我,他们的托底是我前进的基石,以后有工资了要给父母买很多东西最感谢的是我的女朋友,我们从大一相识,一直坚持到大四,她是一个非常优秀也异常坚定的女生,也正是因为她的实力出众早再年初就定好了要去上海的一家外企。我为了也去上海,从暑期实习开始投了不少上海的岗位但无一例外的都被拒之门外,但这期间她从来没有嫌弃过我,反而一直鼓励我相信我,如果说父母的托底是我前进的基石,那女朋友的鼓励和信任则是我前进的动力和方向。在如今这个充满戾气和对立的社会,能找到一个一心一意彼此喜欢的人实在是很难得,我深知这种珍贵所以更会加倍珍惜也感谢自己吧,在经历了无数个失眠的夜晚和面试失败的打击下,最终还是迎来了最好的结果,记得在华为线下面的前几周我几乎回到了高三时期的作息,那真是一段充实美好的时光,好在最后的结果也没有辜负这份努力也想跟所有的牛友说:不要因为一时的失败而自怨自艾,妄自菲薄,只要坚持下去,总会有柳暗花明又一村的惊喜在等待着你,机会总是垂青于有准备的人,要相信否极泰来,相信自己。朋友,坚定地相信未来吧,相信不屈不挠的努力,相信战胜死亡的年轻,相信未来、热爱生命。
小肥罗:有这样的女朋友真是幸福
秋招白月光
点赞 评论 收藏
分享
评论
63
198
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务