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 简单英文 ...
随机推荐
- [.NET] 单位转换实践:深入解析 Units.NET
单位转换实践:深入解析 Units.NET 摘要 在现代软件开发中,准确处理不同单位的转换是一个常见而复杂的需求.无论是处理温度.长度.重量还是其他物理量,都需要可靠的单位转换机制.本文将深入介绍 U ...
- SpringBoot集成MinIO8.3.x 依赖冲突解决,至简之招覆盖spring-boot-dependencies的依赖版本声明
版本声明 SpringBoot 2.6.5 MinIO 8.3.7 报错信息 An attempt was made to call a method that does not exist. The ...
- linux 安装 Ollama 框架
概述 Ollama 是一款旨在简化大语言模型(LLM)本地部署的工具,支持 Windows.Linux 和 MacOS 系统.它提供了一个用户友好的环境,让开发者可以轻松地运行和调优如 Qwen.Ll ...
- 【BUG排查记】HttpUtil和SpringSecurity结合的坑
一.背景 最近为了做微服务高可用和优化上线流程,我参与了一个微服务的改造开发. 主要包括redis切换哨兵模式.接入高可用xxljob集群.配置和升级脚本优化. 二.问题描述 项目改造提测后,测试 ...
- 远程连接Windows
远程桌面连接 限制 1.同网段 (1)服务器关闭防火墙 (2)服务器端 右键点击'我的电脑'进入'属性'点击左侧菜单栏中的'远程设置': 把远程桌面选项设置成'允许运行任意版本远程桌面的计算机连接'. ...
- uniapp实现头部、底部固定,中间滚动的布局
参考地址:https://blog.csdn.net/lll_liuhui/article/details/87923041 <template> <view class=" ...
- Q:查看锁表进程,及杀死所有锁表进程sql
查看锁表进程 SELECT DISTINCT decode(s.inst_id, 1, 'DB1', 2, 'DB2') 数据库服务器, decode(s.BLOCKING_SESSION, '', ...
- 闲话 6.30 -JL 引理
参考了 https://spaces.ac.cn/archives/8679/comment-page-1,有一些增删. JL 引理 首先下面需要应用马尔可夫不等式的另一个形式: \[\newcomm ...
- 「ximagine」业余爱好者的非专业显示器测试流程规范,同时也是本账号输出内容的数据来源!如何测试显示器?荒岛整理总结出多种测试方法和注意事项,以及粗浅的原理解析!
> 由于网页编辑器简陋,无法实现原文档的精心排版,如需原文档可联系... 序 「ximagine」在本篇文章中将介绍「荒岛」目前所使用的显示器测试流程及标准,我们主要使用Calman.Displ ...
- 如何训练LLM“思考”(像o1和DeepSeek-R1一样, 高级推理模型解析
2024年9月,OpenAI发布了它的o1模型,该模型基于大规模强化学习训练,赋予了它"高级推理"能力.不幸的是,他们是如何做到这一点的细节从未被公开披露.然而,今天,DeepSe ...