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 简单英文 ...
随机推荐
- @wraps 修饰器:让你的 Python 代码更加简短可爱 | 从简单实例来认识它
@wraps 修饰器:让你的 Python 代码更加简短可爱 | 从简单实例来认识它 我们在上一篇文章(Python实例来认识并发与并行)中用到了 @timer ,在函数定义时,加上一个小小的 @ti ...
- 记一次 contentInsetAdjustmentBehavior 引发的bug
注:本文同步发布于微信公众号:stringwu的互联网杂谈记一次 contentInsetAdjustmentBehavior 引发的bug 1 背景 项目中使用到了UILable来展示相关的文本内容 ...
- CDS标准视图:销售变现天数 I_DaysSalesOutstanding
视图名称:销售变现天数 I_DaysSalesOutstanding 视图类型:参数 视图代码: 点击查看代码 @AbapCatalog.sqlViewName: 'IFIDAYSSLSOUTSTG' ...
- CDS标准视图:总计应收款 I_TotalAccountsReceivables
视图名称:总计应收款 I_TotalAccountsReceivables 视图类型:参数 视图代码: 点击查看代码 @AbapCatalog.sqlViewName: 'IFITOTALACCTRB ...
- 线上JVM性能优化详解
概述 jvm中除了程序计数器,其他的区域都有可能会发生内存溢出 内存溢出是什么? 当程序需要申请内存的时候,由于没有足够的内存,此时就会抛出OutOfMemoryError,这就是内存溢出 内存溢出和 ...
- Golang-文件处理11
http://c.biancheng.net/golang/102/ Go语言自定义数据文件 对一个程序非常普遍的需求包括维护内部数据结构,为数据交换提供导入导出功能,也支持使用外部工具来处理数据.由 ...
- dicom 在生成dcm文件的时候会出现乱码的处理方法
using Dicom.IO; using System; using System.Collections.Generic; using System.Linq; using System.Text ...
- shell脚本中的循环(for循环,while循环)及break、continue的用法示例
Shell脚本中也算是一门简易的编程语言了,当然循环是不能缺少的.常用到的循环有for循环和while循环.下面就分别介绍一下两种循环的结构. [for循环]: Shell脚本中的for循环示例: # ...
- 告别虚拟机!WSL2安装配置教程!!!
作者:SkyXZ CSDN:SkyXZ--CSDN博客 博客园:SkyXZ - 博客园 由于Linux的系统的稳定以及在环境管理方面的优越性,同时Linux对于ROS系统的独占,很多时候我们都乐意在L ...
- 完整的 c 语言用到的基础知识总结
作者: baron 博客: baron-z.cn 用于记录 c 语言基础知识,如果没有特别说明本文的所有代码编译环境为 gcc 编译器编译,学习资料来自狄泰软件学院可在淘宝购买学习 一.基本数据类型 ...