记一次面试题

参加了一次 黑客排名 的技术面试

得到的经验就是别去看那有的没的的题干,很浪费时间,而是要:

  1. 看示例输入
  2. 看示例输出
  3. 找输入输出的关系
  4. 写框架代码
  5. 找题中的细节要求
  6. 完善代码
  7. Test & Debug

我有 70 分钟作答 ,但是吧,这题弱智都做得出来,无非是时间问题

算法题

List<List<int>> 中找出最大相邻相同数字的方阵长度,比如

输入

1
2
3
1 1 1
1 1 0
1 0 1

输出 2

本题需要用到 动态规划 的思想

二维数组 dp 来记录以每个元素为右下角的最大相邻相同数字的方阵长度
然后遍历输入的矩阵,根据当前元素的值来更新 dp
最后,返回 dp 中的最大值作为最大相邻相同数字的方阵长度

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
static int FindMaxSquareLength(List<List<int>> matrix) {
var rows = matrix.Count;
var cols = matrix[0].Count;
var maxLength = 0;

var dp = new int[rows, cols];

for (var i = 0; i < rows; i++)
for (var j = 0; j < cols; j++) {
if (i == 0 || j == 0)
dp[i, j] = matrix[i][j];
else if (matrix[i][j] == 1)
dp[i, j] = Math.Min(dp[i - 1, j - 1], Math.Min(dp[i - 1, j], dp[i, j - 1])) + 1;

maxLength = Math.Max(maxLength, dp[i, j]);
}

return maxLength;
}

List<List<int>> matrix = new() {
new() { 1, 1, 1 },
new() { 1, 1, 0 },
new() { 1, 0, 1 }
};

var maxLength = FindMaxSquareLength(matrix);
Console.WriteLine("最大相邻相同数字的方阵长度为: " + maxLength);

技能题

编写一个 HTTP GET 查询

URL: https://jsonmock.hackerrank.com/api/transactions
它不提供除了 ?page=num 以外的查询参数

编写代码以实现

1
List<string> maximumTransfer(string name, string city)

其中,找出最大的 credit[0],最大的 debit[1]

这个真没啥好说的,单纯考基本功

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
static async Task<List<string>> maximumTransfer(string name, string city) {
var result = new List<string>();
var allData = await GetAllData();

var maxCredit = decimal.MinValue;
var maxDebit = decimal.MinValue;

var maxCreditAmount = "";
var maxDebitAmount = "";

foreach (var data in allData.Where(data => data.userName == name && data.location.city == city))
switch (data.txnType) {
case "credit": {
var creditAmount = ParseAmount(data.amount);
if (creditAmount > maxCredit) {
maxCredit = creditAmount;
maxCreditAmount = data.amount;
}

break;
}
case "debit": {
var debitAmount = ParseAmount(data.amount);
if (debitAmount > maxDebit) {
maxDebit = debitAmount;
maxDebitAmount = data.amount;
}

break;
}
}

result.Add(maxCreditAmount);
result.Add(maxDebitAmount);

return result;
}

static async Task<List<Data>> GetAllData() {
var allData = new List<Data>();
var currentPage = 1;
var totalPages = 1;

while (currentPage <= totalPages) {
var url = $"https://jsonmock.hackerrank.com/api/transactions?page={currentPage}";
using (var client = new HttpClient()) {
var response = await client.GetAsync(url);
if (response.IsSuccessStatusCode) {
var json = await response.Content.ReadAsStringAsync();
Root root = JsonConvert.DeserializeObject<Root>(json);
allData.AddRange(root.data);
totalPages = root.total_pages;
} else
throw new($"Failed to retrieve data from {url}. Status code: {response.StatusCode}");
}

currentPage++;
}

return allData;
}

static decimal ParseAmount(string amount) {
var cleanedAmount = amount.Replace("$", "").Replace(",", "");
return decimal.Parse(cleanedAmount);
}

var maxTransfer = await maximumTransfer("John Doe", "New York");
Console.WriteLine("Max Credit: " + maxTransfer[0]);
Console.WriteLine("Max Debit: " + maxTransfer[1]);

internal class Data {
public int id { get; set; }
public int userId { get; set; }
public string userName { get; set; }
public object timestamp { get; set; }
public string txnType { get; set; }
public string amount { get; set; }
public Location location { get; set; }
public string ip { get; set; }
}

internal class Location {
public int id { get; set; }
public string address { get; set; }
public string city { get; set; }
public int zipCode { get; set; }
}

internal class Root {
public int page { get; set; }
public int per_page { get; set; }
public int total { get; set; }
public int total_pages { get; set; }
public List<Data> data { get; set; }
}

SQL 题

写一个 SQL 查询,有表

1
2
customers(id: smallint, name:varchar)
warehouses(customer_id, volume: decimal, is_active:smallint)

期望输出

1
record(name, warehouses, min_volume, max_volume, total_volume)

只有 active 的仓库才被列出,按 name 排序

或许 ChatGPT 写的比你更快更好 ,有的时候找找自己原因,好吧,这么多年了工资涨没涨,有没有认真工作,好不,这么多年都是这个代码质量我真的疯掉了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
SELECT
c.name AS name,
COUNT(w.customer_id) AS warehouses,
MIN(w.volume) AS min_volume,
MAX(w.volume) AS max_volume,
SUM(w.volume) AS total_volume
FROM
customers c
INNER JOIN warehouses w ON c.id = w.customer_id
WHERE
w.is_active = 1
GROUP BY
c.id, c.name
ORDER BY
c.name;
Author

Aloento

Posted on

2023-10-25

Updated on

2024-04-04

Licensed under

CC BY-NC-SA 4.0

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×