English interview - three interesting questions of algorithm analysis (英语面试- 三道有趣的算法分析题目)
Background introduction
Here are some problems related to Big-O notation.
From now on, I will try to write blogs in English for my English interview.
But below the English descriptions there will be some translations in Chinese, so you guys who don't know English well can still read my article.
中文:
这是一些关于算法分析的题目。
我最近要开始尝试用英语写博客了,但是我会附上中文翻译在下面。
Upper bound or Lower bound?
Question:
Suppose your friend discovers a new algorithm and in his excitement tells you that his algorithm has a lower bound of O(n2). Can you explain why your friend's statement makes no sense?
Answer:
Let me explain the definition of the two bounds.
The lower bound is the best an algorithm can perform. Oppositely,the upper bound is the worst an algorithm can perform.
And Big O notation cares about the upper bound but is silent about the lower bound.
We can not say in the best case the running time complexity is quadratic or better,may be in linear or constant.
Hence,It makes no sense and it's not logical.
中文解读:
大O表示法关注的是一个最差的情况,但是下界意思是最佳情况,所以下界是O(n^2)这种表述的意思是 在最佳情况下,算法的时间复杂度最差是n^2,这种说法是不合逻辑的。
Formal Definition of Big O
Question:
Does O(2^2n) equal O(2^n) ?
Answer:
First of all, O(g(n)) means a set of functions.
if O(2^2n) equals O(2^n) , it means that two statements are ture at the same time.
- we can find any functions which belongs to O(2^2n) must belongs to O(2^n)
- we can find any functions which belongs to O(2^n) must belongs to O(2^2n)
Let's test the f(n) = 2^2n
if 2^2n = O(2^n) , so the inequality 0 <= 2^2n <= c2^n holds when c is positive and n is greater than n0
let's divide both sides by 2^n, and we can see
0 <= 2^n <= c ,when c is fix, we can find a n' which makes the inequality false.
So O(2^2n) is not equal to O(2^n)
中文解读:
首先翻译一下题目,能想得到若题目条件成立,则必须同时满足两个条件:
- 任何属于2^2n函数集合的函数也都属于2 ^ n集合
- 任何属于2^n函数集合的函数也都属于2 ^ 2n集合
这两个集合就一模一样了。
用反证法证明,若2^2n属于O(2 ^n),最后推出矛盾结果,所以并不相等。
M % N means?
Determine the time complexity for the following snippet of code
void complexMethod(int n, int m) {
for (int j = 0; j < n; j++) {
for (int i = 0; i < m % n; i++) {
System.out.println("")
}
}
}
Answer:
m%n is the key to solve the problem.
if m equals n, m%n equals 0 and the inner loop will not run. The complexity is O(n)
if m is less than n ,m%n equals m and the complexity is O(m*n).
The last case is when m is greater than n, m%n equals value ranging from 1 to n-1, So the complexity will in the worse case be O(n^2)
Hence, the complexity would be O(n^2)
中文解读:
内层循环的余数需要分为三种情况讨论,最差的情况是O(n^2),所以整个算法的时间复杂度是O(n ^2)
English interview - three interesting questions of algorithm analysis (英语面试- 三道有趣的算法分析题目)的更多相关文章
- 《Data Structures and Algorithm Analysis in C》学习与刷题笔记
<Data Structures and Algorithm Analysis in C>学习与刷题笔记 为什么要学习DSAAC? 某个月黑风高的夜晚,下班的我走在黯淡无光.冷清无人的冲之 ...
- English interview!
Q1:Why are you interested in working for our company?为什么有兴趣在我们公司工作?A1:Because your company has a goo ...
- Top English interview Q&A part 2.
https://www.zhihu.com/question/19666878 1.how do you handle failure? I have always lived by the maxi ...
- Top English interview Q&A
http://www.hjenglish.com/new/p581292/ vocabulary endeavour [ɪn'devər] relevant ['reləvənt] , efficie ...
- 30 algorithm questions study
April 26, 2015 Spent over a few months to go over 30 questions about algorithm starting from January ...
- Programming Interview Questions Websites All In One
Programming Interview Questions Websites All In One 编程面试刷题网站 http://highscalability.com/ https://tri ...
- [Algorithm] 如何正确撸<算法导论>CLRS
其实算法本身不难,第一遍可以只看伪代码和算法思路.如果想进一步理解的话,第三章那些标记法是非常重要的,就算要花费大量时间才能理解,也不要马马虎虎略过.因为以后的每一章,讲完算法就是这样的分析,精通的话 ...
- 《Cracking the Coding Interview》——第18章:难题——题目6
2014-04-29 02:27 题目:找出10亿个数中最小的100万个数,假设内存可以装得下. 解法1:内存可以装得下?可以用快速选择算法得到无序的结果.时间复杂度总体是O(n)级别,但是常系数不小 ...
- Algorithm lesson final exam
1.algorithm analysis O B/W/AV/AMOR,混入其他问题,设计+分析 2.传统算法(肯定要考) 1)divide and conquer master therem. rec ...
- Simple English
Simple English 1. Basic English 1.1 设计原则: 1.2 基本英语单词列表850个 1.3 规则: 1.4 质疑 1.5 维基百科:基本英语组合词表 1.6 简单英文 ...
随机推荐
- 使用 NodeLocalDNS 提升集群 DNS 性能和可靠性
本文主要分享如何使用 NodeLocal DNSCache 来提升集群中的 DNS 性能以及可靠性,包括部署.使用配置以及原理分析,最终通过压测表明使用后带来了高达 50% 的性能提升. 1.背景 什 ...
- salesforce零基础学习(一百四十二)在Formula字段中如何通过Datetime字段显示Local Time(适配DST)
背景: 我们需求是显示Date Time类型的Time信息,比如我们想要在report中基于Hour Of Created Date进行分组,从而想要了解到一段时间内什么时间是数据创建的高峰期,不同的 ...
- 特斯拉CEO埃隆马.斯克的五步工作法,怎么提高工程效率加速产品开发?
简介 在<埃隆·马斯克传>这本书中,有两个章节写到了特斯拉 CEO 埃隆马斯克为了在一段时间内,提升特斯拉汽车 model 3 的产能到每个月 5000 辆这个数量级,在书中叫 " ...
- C# HOOK 键盘事件
C# HOOK 键盘事件 /* by: wgscd date:2023-8-15 desc: test hook in c# */ using System; using System.Collect ...
- c# set Webbowser version with WPF/Winform app
<Window x:Class="TestWPF.MainWindow" xmlns="http://schemas.microsoft.com/winfx/200 ...
- SpringBoot(八) - 统一数据返回,统一分页工具,统一异常处理 (生成随机数,正则校验)
1.统一数据返回 使用逆向工程来进行测试,实体,mapper等省略: 1.1 直接使用 RequestResoult 1.1.1 RequestResoult 请求结果返回实体 //统一返回 实体 类 ...
- Kotlin:【继承】:继承、函数重载、类型检测、as类型转换、智能类型转换、Kotlin层次
- 异常try-catch-finally与存储和JSON.parse
捕获异常 捕获异常:处理可能出现的异常,当发生错误后,我们对它进行处理,不让程序崩溃. 异常处理 try-catch-finally try{ // 可能出现异常的:代码1 }catch(err){ ...
- linux下自建NAS教程
NAS,英文全名Network Attached Storage,翻译过来是:网络附接存储. 引用维基百科定义 网络附接存储(英语:Network Attached Storage,缩写:NAS)[1 ...
- STM32IO口模拟IIC时序
正点原子IIC讲解:https://www.bilibili.com/video/BV1o8411n7o9/?spm_id_from=333.337.search-card.all.click& ...