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 简单英文 ...
随机推荐
- minimind复现记录
- [LC515]在每个树的行中找最大值
题目内容 题目分析 这是一道典型的树结构遍历题,可以使用层序遍历(BFS)或者(DFS)进行解题. 在BFS中,使用队列结构遍历树的每一层并维护每层的最大值. 在DFS中,由于并不是一层一层的去访问树 ...
- 从单体架构、到SOA、再到微服务的架构设计详解
本文涉及的内容以及知识点如下: 1.单体架构 2.单体架构的拆分 3.SOA与微服务的区别 4.微服务的优缺点 5.微服务的消息 6.服务集成 7.数据的去中心化 单体架构 Web应用程序发展的早期, ...
- Golang-接口7
http://c.biancheng.net/golang/interface/ Go语言接口声明(定义) Go语言不是一种 "传统" 的面向对象编程语言:它里面没有类和继承的概念 ...
- react 计算衍生数据
import React from 'react' import { connect } from 'react-redux' import TodoList from '../components/ ...
- 在日常工作和生活中使用Linux-开篇
前言 欢迎来到<在日常工作和生活中使用Linux>的系列分享.在这个系列中,我们将探讨为什么选择Linux,以及如何在日常工作和生活中高效地使用它.无论你是刚刚接触Linux的新手,还是希 ...
- 快速 log2 取整算法 (O(1) 时间与空间复杂度)
先上核心代码(文末附针对多种整数类型的代码): inline int log_2(int x) { int rst = 0; if (x & 0xffff'0000U) rst += 16, ...
- Iceberg根据快照查看文件,根据文件查看哪个快照写入
一.背景 用户查询iceberg表时报文件为空,因为存在写入和治理程序同时操作iceberg表,需要查看空文件是哪个快照产生的,方便确定是flink写入缺陷还是spark治理缺陷 二.通过Sql查询文 ...
- 表治理-Iceberg元数据合并-metadata.json文件
一.背景描述 元数据文件随时间增多,导致查询变慢.通过如下方式可以指定metadata个数,超过指定数量自动清理. metadata文件对应Iceberg概念是Snapshots 二.解决方案 1.在 ...
- StarUML画类图
1.Classes说明 [1]Class 类 [2]Interface 接口 [8]Generalization A与B的泛化关系,A继承B.继承非抽象类 [9]Interface Realizat ...