没有用的大模型面试coding题集(一)

“没有用”的意思,就是没有用,就算会了这些,面试的时候也不会真的被问到。

然而为什么文章是这个鸟title,因为这是我面试时会出的题,而且根据我的经验,80%的候选人,即便是用最基础的方法也没办法在30分钟内写出来。但凡能在30分钟内用最基础的办法写出来的,我都认为是满足了训练大模型的最低标准的。

当然知乎人均3秒钟写出来,所以请不要嘲讽问题简单,或者参考答案的愚蠢,言而总之,

所以如果您要是也觉得没有用,您是对的。

I. 转置一个嵌套列表(transpose a nested list) 问题:

1
2
3
4
5
6
# Python

# 给定一个嵌套列表 foo = [ [A, B], [C, D] ],
# 将其转置为 bar = [ [A, C], [B, D] ]。
# 被嵌套的可以是list,也可以是tuple等其他iterable的对象。
# A的type是任意的,甚至也可以是list。

参考答案:

1
bar = list(zip(*foo))

拓展问题:

1
# 如果foo中的子列表不等长,应该如何按照最长/最短去做转置?

II. 分组列表中的元组(group tuples in a list) 问题:

1
2
3
4
5
6
# Python

# 给定一个tuple的列表 foo = [ (A, 1), (B, 2), (C, 2), (D, 1) ],
# 根据tuple的最后一个元素,将列表分组为 bar = [ [ (A, 1), (D, 1) ],[ (B, 2), (C, 2) ]] 。
# tuple的长度不确定,tuple内的元素type也是任意的,
# 尽量调用官方的library。

参考答案:

1
2
3
4
5
6
7
8
9
10
from itertools import groupby

# 按照分组元素排序
sorted_foo = sorted(foo, key=lambda x: x[-1])

# 调用官方黑魔法
grouped_data = groupby(sorted_foo, key=lambda x: x[-1]) 

# 展开为列表
bar = [ list(f) for _, f in grouped_data ]

拓展问题:

1
# 如何给予tuple中最后一个元素的type来进行分组?

III. 选择中值(pick the median value) 问题:

1
2
3
4
5
6
7
8
# Python

# 有一个列表中存有一些-10.0~10.0的float数,
# 与此同时这个列表中也混有一些None值。
# 例如, foo = [ -1.0, None, 1.0, None, 0.0 ]
# 现在要求返回排除掉None值后的中值index,
# 例如在foo中,中值是0.0,则返回 bar = 4。
# 如果有偶数个有效值则取偏大的那个。

参考答案:

1
2
3
4
5
6
7
8
9
10
11
import numpy
foo = [ -1.0, None, 1.0, None, 0.0 ]
# 统计None的个数
n_none = foo.count(None)

# 把None替换为一个大数,保证其不干扰正常数值的排序
foo_fix_none = [255.0 if i is None else i for i in foo]

# 移除掉None的个数并计算index
index = (len(foo) - n_none) // 2
bar = numpy.argsort(foo_fix_none)[index]

拓展问题:

1
# 如何找到中间的K个值的index?

IV. 寻找特定number pattern(find the position of a number pattern) 问题:

1
2
3
4
5
6
7
# Python

# 给定一个列表中存有一些正整数(保证有且只有正整数),
# 寻找最后一个数字1的index,要求这个数字1后面存在数字2(保证存在性)。
# 例如,foo = [0, 1, 2, 1, 0, 1, 0, 2, 1, 0],
# 则应该输出 bar = 5。
# 不限制各种library的黑魔法。

参考答案:

1
2
3
4
5
6
7
8
9
10
import numpy
foo = [0, 1, 2, 1, 0, 1, 0, 2, 1, 0]
# 把所有1的index枚举出来
index_of_all_one = numpy.arange(len(foo)) * (numpy.array(foo) == 1)

# 寻找最后一个2的位置
index_of_last_two = numpy.where(numpy.array(foo) == 2)[0][-1]

# 截取2之前的index,找到最后一个有效1
bar = numpy.max(index_of_one[:index_of_last_two])

拓展问题:

1
# 现在有一个2D的numpy array,要求针对每一行都进行上面的操作,但是不能现式的进行loop。

V. 依照subprocess rank打印log(print log ordered by subprocess rank) 问题:

1
2
3
4
5
6
7
# Python

# 一个程序启动了N个subprocess,这些subprocess拥有从1~N不同的rank。
# 程序中定义了函数 get_rank() 可以返回当前subprocess的rank。
# 实现一个方法 foo(bar),使得这些subprocess可以按照rank的顺序打印bar,
# get_rank()函数中内嵌了barrier功能,使得所有subprocess在此刻同步。
# 不需要考虑实现的成本。

参考答案:

1
2
3
4
5
6
7
8
9
import time

def foo(bar):
    rank = int(get_rank())

    # 每一个subprocess各自睡眠rank秒用来调整顺序
    time.sleep(rank)

    print(bar)

拓展问题:

1
# 如何让这些subprocess可以按照rank的顺序写入一个log文件?



Enjoy Reading This Article?

Here are some more articles you might like to read next:

  • Nanshan Jokes Collection (Gemini 2.5 Pro Translated Version)
  • 南山笑话集锦
  • Some Stray Thoughts After Leaving the Large Model Industry (Gemini 2.5 Pro Translated Version)
  • 离开大模型业界后的一点杂念
  • Large Models and Coin Minting, Continued (Gemini 2.5 Pro Translated Version)