.net趣味面试题总结
1.算法面试题:一个List,要求删除里面的男生,不用Linq和Lamda,求各种解,并说明优缺点!
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace ConsoleApp1 { class Program { #region 初始化 static readonly string text = "1000100010101001101101011110111111100000100010001101010101001101111010100001010101100101011011101011111010101000111100110101001001000010100000010111010110011010111100000101110011011100100010110101100010111010100010110000001011110110000011110001101001000000111100110101101111111000011100010110010000000101111010111011100000101111000011101101110100111010101001010000001101000100111011111101010100011011000001001010100100001001011001100011001010111110110100101101101010010100010101011100110101110011011101001001001110101101011010101001000011001111010101110101010001111010110001011100010001000011000101000011010011011000100011111110111010001111111000100011110000100010010101000111110111111101111111000000001011010101000100100101010100000111101111010011011011100000001001000110011100110110001110100100110010011100000110111010010011100011001100010001011100010010001011100110101110000001000010001111101010110000111111110101110100111101110000111000110011110101001110010001010100110110010100011110110111010010011011100011010111011010100110011010001110011101000100010100011110001000101010100000010111011010101100001111011000001110110010011111011000010001101100001101100001010010000001001101110101110001100000100100100110101110110110101001111011011010111000100110011011111001010001100001011000110000100011000010010101011010100111001000110001011101010100010101111101100101001110100111000011110101111111001011100001001001100110011001010001111000110000101111010101010011101010111000000001001111001111101010101010101101100101010111001011100000010001011001000001110101000110111000011110101010110101101001001101001110000010000000010011111110101001111111000100100010001100101111011111000110100000000000110010011101010010011100000000111111000001011100000101100001100010101111010110000100011101101101111011100010011111100100100001001001011001000000010100110101110101101100001000011110001100010101011000100000000011101001101101100101010011000101100100001010100011100111110011100110110011100011100011110001101100000001111011100111110011111100001100001111101101111101110001111011010100011000101000111110110111011000011110010110010010100011100101111000101110010110100000011010000101110100001101001001110010110101101011110110001111101101000000011011010001111000010110000011110100101011011100100111001101001101110011101010001001001010010111100010111111111001101010000011000000000101111101011110111001001000010100000010100000110101001101100001111001011100000101010000010000111101100111011101001111000011110111011100011101110000101111000111011101010100000110011000010001101101110000111010111100001011110001101100001000110101010010001010000000001110111011110001110101000000101110010100110010110101011100000100010011100001001010100110100100101111010111100000101010100100101001010110000101110110010110100101101111000010100001010011111111010011101001001011001000101000010000000100111001011010110000010001110010011100010111111100101001010100111000100011001111100101100011100000101100110010101100110111100000001111111011000100010100110111011011001000010110100000001111011010001010101110010111101000010110001010110011001010100101010001110100110100111101010011011110011101011111100101011010101101010010000100110111000011011110110000010110110000001010101011110001110001101100110111011010001001010011110100011111110110110000000111100111100111001110111011011010010100101110001111001110001100110011111001001000110011101111111100111011001001010011101100010000111010111000111101010101100010100101000001000111000010001010110110100010100011010110111110111111101001101000010111000111111011001100001111110011111100000110100110000011010110100111001001100111001010011011011000111100001001111100100101101101010111101001101001000101000100101000010110010100001111001010010111101001100011100010111001110100111111101110011100000011110110100001100001011110110100011010010001100000011111110110001001101001010010010000001000111000110101111000011110111011111010100010011011100111010101101010101000000111001111101001001010011011001101111000000111111111111101001011011000001100110111100011100110010111001111001011100001100001100000001010100011010101111101111011010001100010000001101110100111111100111011010110101000110111101001011010100001101001010011110011111101010100010110111001000001110101001111000001100100110100100010110001111001100011111010000101011101001100001101110111111001111100010110101010110011011100101110100001001110011101101011011010100011100110101100000101001111101101000011110000110011011110100111110000001000010100011000110100000111100011101101000000111111101010000111101100100110001011111100011101010011001000001001000111011111101000110111011000110100110000010100001000100101010110011010011110100100000000001000101011000001010011011011110101110001001101110001100110101011001110010111000101001111101011011011000100100011100110100011000111111110100101000001101000000111101111101100000111011110111001101010011010011011010001100010010100101001000111111110001100100001001111010000101010101111011000111111011101110100110010100000001010000101011101011001110111001100010100101111010111100100010111011110100010001001010100110011110110010110101010010011111010011111111101111100110110000101110000110101100111110111011000001101110100110011010010110001111110001110100011011101110000100101110110101010100010110111100010100110100101110001001011011010100001001110100100110101110110100111010010011001001010000101010100100100100101110101001000100110110100011010101101010101011011001001000001101101000000100001011010110010010111010111101001011100110000100010110000100001100110011110101101000101011100010111111000100011111111111111001101011111010100100100010001000111111010110001111000110011111001001100100000010010101100000101011000001101111001010110000001100111111001111101101111101001011110101011111101100010100100100100110001110011010101111110100000011000100111101110010001001101010001101100110010101111011011001001010010101111100100001111010101001010000001001010110101111100111111101001101011101001100101000001000000001001110100010011001110110010011010000011100011011110100010111111011001000001101100111111110110111111111011110110100100100011010000111110001101011000100100110010011101111001010011100100101001001110001000101010000001100000111101000111011001000101110100010101000110100101100001100011111010001010100111001111111111000100111001110011010101101001010111110000110111010000100011000000000101010000101011100010100111101110110010100111001111000101110011111001100101011101011110101011010010111000101100000001100000001001011011111110101000100101110111001001111100000010110110010011001111101110101010000011110001010100001010010111101010011101100010011110111001111110111110000110001011110101001100110101000000010111101011100000010110101010101001110011011001100101001011111101010110101010010111010101110101010001111001111110010111001000100001010101011100111010100001000100111110101111000010000001111111101001010110000101111101110000011011110001011010101100110111001000010110001011001100101110100101100111001000000000111111010110010111011000100100001110110100010001010101110100110111100111010011000001011101001010111011101101000001000111110000011011011010100011111110101100111111101001001001111001100001001110011100001011111001000001011000010100100110101001111100111001000000001000111001000000000010011000111000111111010100000001101010001001010001011011101100010110111010000110001110000110100001001011101001001011111000111010010101101111000100101010001101110110010011011110000101111111111110111101010100101101000011010001101101100101011100011011101100110101011100110100010101011000010110110111101001001101111101011010111010101100010001001010011111100010100111101100001100100111111011110101111111111000100000001001010110011001101001000001111000100110110001110010001011110010110001010110001011110010010000000100100001111111001110011111111000111010000001100000000011001110100000011101110000100100110110101111110101100101100001000001001000100001011110100000011010100111001110100111110101010011010010011101000111011000111001111011111100010110100010101001010100110100000000111111100011010110001111010000100001110000010100111101101001101100110111010010101110101010110011000011000111010100011100000001111110100110010000111000010001010011111011101000110000011100100111000100000111011010101000110010111100010111010011101001000111001101000001110100010110101100101111111000001110010100011111101001110110101100011101100100001111001001111110000001011000100001100110001001101000111111001100001010111010001001011011111110011111110001000110111101011011101010000001100010000010010011010110010100010000101010100000101010100010011010101010100110111000001110100100100011100101000000010001011010001011111110111110000110110111000010010001000000101000000100101110110111011010010111000000101110001001000010010111110111010110000000111101010111111011000001010010010001101010000111101000111011100111011001010011001001101000111111111110000110100110101110110010010100011100001000010001001011111110001111111001100001110110011111110100000011110010011110111111111011000001111101100110111011101000100111001000000101100101101001100111110100111010010110101010111101001000010111010011000010000110111100011011110000010011111001010100100000100000100110000100111000110001010100101001001011010000011010110101100100101011010000001010000011110100101001101000100100001000101100100110101111001001010110101010011111100000111110011101011000001101011000010000001110010101010100000111000100011111100101000010011010110111110011100010111101100101111101101110000100001100010101011101100001101000011110100100101100110000011010010100000011111000111111001010010011110110011010100100010111110110100001101110101111110001000000001000111111011011010000000010010111101011110011100000111000000100111001010010011110010010100100100010101100001001011011101110011110001000011011001001011100111101000100101110110110011000101010010111000111111011111100000010011010101110000101000001110011001001011111100011111010100111100010110001111010100010100001110000101101100000111100101000011010010010111010111001011101011011000111"; #endregion private static List<Task> tasks = new List<Task>(); static void Main(string[] args) { List<int> list1 = new List<int>(); for (int i = 0; i < text.Length; i++) { list1.Add(text[i] - 48); } #region 方案1(120分答案):创建新列表 免扩容拷贝。新列表容易写进去 避免了删除时候的内存拷贝 //创建新列表 免扩大容量 List<int> list2 = new List<int>(); //1.为什么使用循环添加 而不是remove删除 //因为使用remove删除时候会产生内存拷贝,并且数据还会重新排列 foreach (int item in list1) { if (item == 0) {//添加为0状态的值 list2.Add(item); } } #endregion #region 方案2(100分答案):使用for循环 从后面往前删除 避免从前面往后面删除 因为这样会造成数组从新排列 for (int i = list1.Count()-1; i >= 0; i--) { if (list1[i] == 1) {//删除1 实际得到了0 list1.Remove(list1[i]); } } #endregion } } }
2.堆和栈的区别
进程堆,线程栈的区别?
1.堆 Heap: 进程堆,一个程序在运行是,进程对方引用类型变量的一块内存,全局唯一
2.栈 Stack: 线程栈,一个线程存放变量的一个内存,随着线程的生命周期存在的
那么引用类型和值类的区别呢?
1.引用类型:类/接口/委托 存放在堆上
2.值类型:结构/枚举 存放在栈上
因为值类型和引用类型的分配就是内存分配,内存分配是有上限的,所以就产生了GC
托管堆垃圾回收 -- GC
1.值类型和引用类型在GC的区别
只有引用类型才需要垃圾回收 存放在托管堆上 值类型是存放在栈上的 一直存在内存中
2.托管资源和非托管资源的区别
托管在CLR上 如:new的对象,string
非托管资源:数据连接,文件流,句柄 非托管资源是需要手手动释放;
using 其实是c# 封装了非托管资源的连接
3.什么对象的内存,会被GC回收
对象访问不到了,就可以被GC回收
程序入口--找对象--建立一个标记--对象图---如果访问不到的就是垃圾
4.对象是如何分配再堆上
引用类型每次分配在堆上面的时候回检查当前空间是否足够 足够就分配,不足够就执行GC
5.什么时候会执行GC
1 程序在退出的时候会自动执行GC
2 string a="value" 设置 a=null; 会执行GC
3 创建对象的时候--会有一个临界点 当0代对象内存满了 GC会自动去清理0代对象
4 可以手动触发 GC.Conllect 然后直接执行GC 但是不建议频繁的去GC 因为回收也是需要消耗资源的
6.GC回收流程
首先将所有对象--全部对象标记为垃圾--开始一个一个检查,如果可以访问到,就标记一下为不是垃圾对象
然后会再次遍历去清除内存 清除之后产生不连续内存---然后进行地址移动--在压缩--最后修改变量指向
7.垃圾回收的策略
对象分代:3代
0代: 第一次分配到堆 就是0代
1代: 被回收一次之后就由0代变成1代,依然存在
2代: 被回收两次之后就由1代变成2代,依然存在
优先回收0代,提高效率,最容易也是多需要回收的
如果0代回收之后,内存仍然不够----就去找1代 -----2代
8.大对象:所存储的大小大于等于80000个字节的时候,同时大对象会被直接列举为2代
面向对象:封装继承多态
封装:确定好对象,隔离业务逻辑,提高代码重用
继承:子类继承父类,代码重用
多态:一个父类被多个子类继承,每个子类有不同方法,
接口和抽象类的区别
1.抽象类不能被实例化,只能继承一个抽象类,包含各个抽象方法,一般在创建组件的多个版本,则创建抽象类。抽象类提供简单的方法来控制组件版本
2.接口是一个行为的规范,能被多个类继承,不能被实例化,在创建的功能将在大范围的全异对象间使用,则使用接口。如果要设计小而简练的功能块,则使用接口
list<T>和list[]的区别
1.List<T> 泛型数组是存储可变的不固定长度的对象
2.List[],存储的是固定长度的对象
分布式锁
1。什么时候使用分布式锁:如果需要对某一个共享变量进行多线程同步访问的时时候
2.三种使用方法:redis,数据库加标记,ZooKeeper是一个为分布式应用提供一致性服务的开源组件,它内部是一个分层的文件系统目录树结构,规定同一个目录下只能有一个唯一文件名
分布式事务
1:银行转账给别人, 自己卡扣钱,别人卡加钱,不在同一个节点上,就涉及到分布式事务
四种解决方案:RocketMQ事务消息消息队列,补偿事务(TCC),本地消息表(异步确保),两阶段提交(2PC)
4:请问你在地址栏输入www.****.com发生了什么
- 1:浏览器地址栏输入url
- 2:浏览器会先查看浏览器缓存系统缓存路由缓存, 如有存在缓存, 就直接显示。 如果没有, 接着第3步
- 3:域名解析(DNS)获取相应的 IP
- 4:浏览器向服务器发起 tcp 连接, 与浏览器建立 tcp 三次握手
- 5:握手成功, 浏览器向服务器发送 http 请求, 请求数据包(请求行,请求报头和请求正文。)
- 6:服务器请求数据, 将数据返回到浏览器
- 7:浏览器接收响应, 读取页面内容, 解析 html 源码, 生成 Dom 树
- 8:解析 css 样式、 浏览器渲染, js交互
高并发高可用:https://www.cnblogs.com/LZXX/p/13530881.html
1.缓存穿透
(请求过来的时候-缓存和数据库都没有):虽然数据都没有但是大量并发的时候 请求还是要进到数据库并且还要返回结果给人家。这样人家恶意攻击就难受了
怎么解决:如果数据没有,缓存就存一个null,下一次来的时候返回null,这样请求就到不了数据库
2.缓存击穿
(请求过来的时候-缓存没有数据库有):大量并发请求还是直接到数据库,
怎么解决:这个问题不大,下一次进来给他缓存就好,或者进行缓存提前预热
3.缓存雪崩
(请求过来的时候-数据缓存同时过期):请求还是到数据库来了,
怎么解决:数据设置随机过期时间,别都设置为30分钟过期,这样就降低缓存雪崩出现
SQL聚集索引和非聚集索引的区别
1:聚集索引只有一个,存储几率是物理上的连续存在, 索引的叶节点就是数据节点
2:非聚集索引有多个,是逻辑上的连续,物理存储并不连续,叶节点是索引节点,但会有一个指向最终数据的指针
他们的数据结构是B树,
吞吐量::QPS(TPS)、并发数、响应时间
QPS(TPS)= 并发数/平均响应时间
并发数 = QPS*平均响应时间(系统同时处理的request/事务数)
响应时间: 一般取平均响应时间
QPS:每秒查询率 , 规定时间内处理了多少流量
TPS:事务数量/秒,发送请求开始计时,收到服务器响应后结束计时, 来计算 使用的时间和完成的事务个数
例:访问一个页面会请求服务器三次,响应产生一个TPS,产生3个QPS
.net core 管道模型
所谓管道,就是拿着HttpContext,经过多个步骤的加工,生成Response,而这其实就是管道模型请求
发送http请求>被Kestrel解析得到HttpContext->然后被后台代码处理Request>返回Response->经由Kestrel回发到客户端
Sql调优
首先拿出一段sql语句通过执行计划查看哪里进行了多次全表扫表和嵌套循环,
1、在表中建立索引,优先考虑where、group by使用到的字段。
2、尽量避免使用select *,返回无用的字段会降低查询效率
3、尽量避免使用in 和not in,会导致数据库引擎放弃索引进行全表扫描。如下:
4、尽量避免使用or,会导致数据库引擎放弃索引进行全表扫描。如下:
MVC 过滤器
最先开始执行的是:
认证过滤器:IAuthenticationFileter
授权过滤器:IAuthorizationfiler 继承 AuthorizeAttrivute
动作过滤器:IActionFilter 继承 ActionFilterAttribute
结果过滤器:IresultfIlter 继承 ActionFIlterAttribute
异常过滤器 :IExceptionfilter 继承handleErrorAttribute
高并发高可用
1.刚开始进行分布式拆分,加缓存 垂直扩展就是加硬盘升级服务器,水平扩展就是拆分功能,付款,订单,拆出来
2.进行第一次分布式架构设计,数据库,应用服务,文件服务各一台服务器
3.然后热点数据加缓存,(出现缓存延迟问题:数据库更新了,页面没更新,给缓存加过期时间)
5.开始进行集群和负载均衡策略, 一台服务器不行 集群增加高可用,负载均衡解决了高并发
6.在不同的服务器用户持久化(登录服务器1 保存session,访问服务器2不认识怎么办)用1.存数据库,或者redis来解决,或者请求携带
7.再进行数据库的读写分离,使用发布订阅模式+代码访问的时候进行负载均衡策略,分库分表
本文来自博客园,作者:12不懂3,转载请注明原文链接:https://www.cnblogs.com/LZXX/p/13469529.html