Pramp - mock interview experience

本文介绍了一种高效的算法来查找两个美国社保号数组中的重复项。通过对比两种不同的情况:数组长度相近与相差悬殊时的最佳解决方案。文章讨论了暴力破解与优化后的算法,并分析了各自的复杂度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Pramp - mock interview experience

 
February 23, 2016

 Read the article today from hackerRank blog on facebook:

http://venturebeat.com/2016/02/18/how-i-landed-a-google-internship-in-6-months/?utm_content=buffer5c137&utm_medium=social&utm_source=facebook.com&utm_campaign=buffer

  Pramp - free mock interview website - Great! give it a try!

 Julia never had chance to give other people code interview before, and then, she will have one on Pramp. Excited!

 Algorithm for the interview being an interviewer:

  Find The Duplicates

Given two arrays of US social security numbers: Arr1 and Arr2 of lengths n and m respectively, how can you most efficiently compute an array of all persons included on both arrays?
Solve and analyze the complexity for 2 cases:
1. m ≈ n - lengths are approximately the same
2. m ≫ n - one is much longer than the other
 
 Julia worked on the question by herself first, around 20 minutes (no coding) before she reads :
 
1. Brute force solution, time complexity: O(nm), go through two loops, in other words, go through Arr1, and for each element in Arr1, check if it is in Arr2. 
 
2. Can we do better than O(nm)? Of course. 
case 1: m ≈ n - lengths are approximately the same
  sort Arr1, it takes time O(nlogn); 
  and then, for each node in Arr2, find it is duplicated one in Arr1. Use binary search, each search takes O(logn), m elements, so time complexity is O(mlogn)
 
  So, time complexity in total: O(nlogn) + O(mlogn) ≈  O(nlogn), which is better than brute force one:  O(nm) time complexity
  <-   Julia, you missed another important step: Ask if the arrays are sorted or not? 
  <-   Julia, if two arrays are sorted, what is the optimal time complexity? 
        Linear time <-   O(n+m) <- you missed the opportunity to ask yourself the question. 
 
What a fun night to play algorithm again. 

转载于:https://www.cnblogs.com/juliachenOnSoftware/p/5213178.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值