Amazon OA 面试真题解析:Friend Groups、移除 k 个元素后的最少唯一整数、字符串计算器

27次阅读
没有评论

Question 1

There are n people at a party. Some of them are friends and others are strangers.

A person naturally moves to create or join a group containing friends and friends of friends. A friend group is defined as a group of people among which each person is a friend to at least one other person.

Assuming a person is not a friend of anyone outside their group, return the total number of friend groups that may form at the party.

You are given an n x n matrix isFriend where isFriend[i][j] = 1 if the ith and the jth person are friends, and isFriend[i][j] = 0 if they are strangers.

Question 2

Given an array of integers arr and an integer k, find the least number of unique integers after removing exactly k elements.

The count of unique (non-duplicate) integers remaining in the final array is minimum.

Example

Input: arr = [4,3,1,1,3,3,2], k = 3
Output: 2

Question 3

Implement a calculator that takes strings as inputs and operations. It should perform +, -, *, /.

The output should be a string. The system is memory constrained and cannot handle more than one digit at a time.

Cannot do int("100000000000000000000000") + int("7000000000000000000000000").

这组题目都很典型地考察了基础数据结构与常见面试建模能力:第一题本质是用图的连通分量来统计朋友圈数量,通常可用 DFS、BFS 或并查集;第二题则是先统计每个整数出现次数,再按频次从小到大贪心删除,优先移除出现次数少的数字,才能让剩余唯一整数最少;第三题强调字符串形式的大整数运算或逐位计算,核心是不能直接依赖超大整数转换,而要按字符模拟加减乘除或实现高精度计算。

正文完
 0