CS-OA cs-vo Faang

Goldman Sachs面试题:实现运行长度编码和平衡括号检测 – 面试代面 – 面试辅助

近期Goldman Sachs,JPMorganChase, citadel optiver 等金融对冲科技公司都开放了intern岗的招聘。后续我们也会着重更新citadel optiver等hedgefund的面试最新状况。今年我们已经帮多位同学拿过相关公司的offer了哦。

今天我们首先来看看刚刚进行的一场Goldman Sachs的面试真题吧。

整场面试大概是40分钟,2道算法题,需要在规定时间内做完。

问题 1:实现运行长度编码

首先Goldman很温柔的给候选人了以下提示,时间充裕的话一定要改进哦。

/**

  • Instructions to candidate.
  • 1) Run this code in the REPL to observe its behaviour.
  • 2) Consider adding some additional tests in doTestsPass().
  • 3) Implement rle() correctly.
  • 4) If time permits, try to improve your implementation.
    */

/**
*

  • Implement a run-length encoding function.
    *
  • For a string input the function returns output encoded as follows:
    *
  • "a" -> "a1"
  • "aa" -> "a2"
  • "aabbb" -> "a2b3"
  • "aabbbaa" -> "a2b3a2"
  • "" -> ""
    *
    */

func encode(_ str: String) -> String
{
// TODO: Implement solution
}

问题描述: 实现一个运行长度编码函数。对于给定的字符串输入,函数应返回其编码后的输出,格式如下:

  • "a" -> "a1"
  • "aa" -> "a2"
  • "aabbb" -> "a2b3"
  • "aabbbaa" -> "a2b3a2"
  • "" -> ""

参考方案:

  1. 初始化变量: 创建一个空字符串用于存储结果,一个计数器用于统计字符出现次数。
  2. 遍历字符串: 从左到右扫描输入字符串,统计连续字符的数量。
  3. 拼接结果: 当遇到不同字符时,将前一个字符和计数拼接到结果字符串中,并重置计数器。
  4. 处理结束: 扫描结束后,将最后一个字符和计数拼接到结果字符串中。

问题 2:平衡括号检测

问题描述: 编写一个函数检测输入字符串中的括号是否平衡。输入字符串只包含字符 '{' 和 '}'。

  • "{}" -> 平衡
  • "}{" -> 不平衡

// Question 2:
// Balanced Brackets
// Input String : {}
// {} -> Balanced
// }{ -> non Balanced String
// Write a function(isBalanced), return type is boolean, input param == String

参考方案:

  1. 初始化栈: 使用栈数据结构来存储左括号。
  2. 遍历字符串: 从左到右扫描输入字符串。
  3. 处理括号:
    • 如果遇到左括号 '{',将其压入栈中。
    • 如果遇到右括号 '}',检查栈是否为空。
      • 如果栈为空,说明右括号没有匹配的左括号,返回不平衡。
      • 如果栈不为空,弹出栈顶元素,匹配左括号。
  4. 检查栈: 遍历结束后,检查栈是否为空。
    • 如果栈为空,说明所有左括号都有匹配的右括号,返回平衡。
    • 如果栈不为空,说明有未匹配的左括号,返回不平衡。

结论:

Goldman Sachs展示出优秀的算法设计和问题解决希望本文对你有所帮助,祝你面试顺利!

With our powerful interview assistance, candidates can effectively analyze and communicate their solutions to these interview questions. This not only demonstrates their programming abilities to the interviewer but also showcases their clear problem-solving approach and effective communication skills. These abilities are valuable not only for Goldman interviews but also for solving real-world programming problems. We wish you all the best in your interviews!

If you need our interview assistance services, please contact us immediately.

我们可以面试辅助的形式大大提高您面试的成功率,专业护航候选人,使您求职路上多加一重保险,面试代面,请联系我们

Leave a Reply

Your email address will not be published. Required fields are marked *