特斯拉pre-test (Go)

特斯拉pre-test (Go)

  • [1 Q1](#1 Q1)
  • [2 Q2](#2 Q2)
  • [3 Q3](#3 Q3)

1 Q1

原文:

You are given an implementation of a function Solution that, given a positive integer N, prints to standard output another integer, which was formed by reversing a decimal representation of N. The leading zeros of the resulting integer should not be printed by the function.

Examples:

  1. Given N = 54321, the function should print 12345.
  2. Given N = 10011, the function should print 11001.
  3. Given N = 1, the function should print 1.

The attached code is still incorrect for some inputs. Despite the error(s), the code may produce a correct answer for the example test cases. The goal of the exercise is to find and fix the bug(s) in the implementation. You can modify at most three lines.

Assume that:

N is an integer within the range 1...1,000,000,000.


翻译:

bash 复制代码
给你一个函数解决方案的实现,给定一个正整数N,将另一个整数打印到标准输出,该整数是通过反转N的十进制表示形式形成的。结果整数的前导零不应由函数打印。
示例:
	1.给定N=54321,该函数应打印12345。
	2.给定N=10011,函数应该打印11001。
	3.给定N=1,函数应该打印1。
附加的代码对于某些输入仍然不正确。尽管有错误,代码可能会为示例测试用例产生正确的答案。练习的目标是找到并修复实现中的bug。您最多可以修改三行。
假设:
	N是[1...1,000,000,000]范围内的整数。

截图:

代码:

go 复制代码
package main

import "fmt"

func Solution(N int) {
	var enable_print int
	enable_print = N % 10
	for N > 0 {
		if enable_print == 0 && N%10 != 0 {
			enable_print = 1
		} else if enable_print == 1 {
			fmt.Print(N % 10)
		}
		N = N / 10
	}
}

2 Q2

原文:

An infrastructure consisting of N cities, numbered from 1 to N, and M bidirectional roads between them is given. Roads do not intersect apart from at their start and end points (they can pass through underground tunnels to avoid collisions).

For each pair of cities directly connected by a road, let's define their network rank as the total number of roads that are connected to either of the two cities.

Write a function:

func Solution(A \[\]int, B \[\]int, N int) int

that, given two arrays A, B consisting of M integers each and an integer N, where Ai and Bi are cities at the two ends of the i-th road, returns the maximal network rank in the whole infrastructure.

Examples:

  1. Given A = 1, 2, 3, 3, B = 2, 3, 1, 4 and N = 4, the function should return 4. The chosen cities may be 2 and 3, and the four roads connected to them are: (2, 1), (2, 3), (3, 1), (3, 4).

In the pictures below, the chosen cities and roads connected to them are marked in red.

  1. Given A = 1, 2, 4, 5, B = 2, 3, 5, 6 and N = 6, the function should return 2. The chosen cities may be 1 and 2, and the two roads connected to them are: (1, 2), (2, 3).

Write an efficient algorithm for the following assumptions:

N is an integer within the range 2...100;

M is an integer within the range 1...4,950;

each element of arrays A and B is an integer within the range 1...N;

A and B have equal length;

each road connects two distinct cities;

two cities are connected by at most one direct road.


翻译:

bash 复制代码
给出了由N个城市组成的基础设施,编号从1到N,它们之间有M条双向道路。道路不会在起点和终点相交(它们可以通过地下隧道以避免碰撞)。
对于每一对由道路直接连接的城市,让我们将它们的网络排名定义为连接到两个城市之一的道路总数。
写一个函数:
        func解决方案(A[]int, B[]int,N int)int
给定由M个整数和一个整数N组成的两个数组A、B,其中A[i]和B[i]是第i条路两端的城市,返回整个基础设施中的最大网络排名。
示例:
        1.给定A=[1,2,3,3],B=[2,3,1,4],N=4,函数应返回4,选择的城市可能是2和3,与它们相连的四条道路是:(2,1),(2,3),(3,1),(3,4)。
        在下面的图片中,选择的城市和与之相连的道路用红色标记。

        2.给定A=[1,2,4,5],B=[2,3,5,6],N=6,函数应返回2,选择的城市可能是1和2,与它们相连的两条道路是:(1,2),(2,3)。

        为以下假设编写一个有效的算法:
                N是范围[2...100]内的整数;
                M是[1...4,950]范围内的整数;
                数组A和B的每个元素都是[1... N]范围内的整数;
                A和B长度相等;
                每条道路连接两个不同的城市。
                两个城市最多只有一条直达道路。

截图:

3 Q3

原文:

Write a function Solution that, given an array A consisting of N integers, returns the number of fragments of A whose sum equals 0 (that is, pairs (P, Q) such that P ≤ Q and the sum AP + AP+1 + ... + AQ is 0). The function should return −1 if this number exceeds 1,000,000,000.

Examples:

  1. Given A = 2, −2, 3, 0, 4, −7, the function should return 4, as explained on this picture:
  2. Given A = 0, 0, ..., 0 of length 100,000, the function should return −1.
    Write an efficient algorithm for the following assumptions:
    • N is an integer within the range 1...100,000;
    • each element of array A is an integer within the range −10,000...10,000;

翻译:

bash 复制代码
编写一个函数Solution,给定一个由N个整数组成的数组A,返回A的总和等于0的片段数(即,对(P, Q)使得P≤Q并且总和A[P]+A[P+1]+...+A[Q]为0)。如果此数字超过1,000,000,000,则该函数应返回-1。
示例:
1.给定A=[2,−2,3,0,4,−7],函数应该返回4,如下图所示:
2.给定长度为100,000的A=[0,0,...,0],该函数应返回-1。
为以下假设编写一个有效的算法:
 N是范围[1...100,000]内的整数;
 数组A的每个元素都是[−10,000...10,000]范围内的整数;

截图:

相关推荐
江湖十年36 分钟前
Go GOPROXY 兜底链的坑:从一次诡异的 CI lint 失败说起
后端·面试·go
尾善爱看海10 小时前
Vue 面试收官篇:SSR、性能优化落地、30 道高频面试题精讲(附标准答案)
前端·javascript·vue.js·面试·vue
DeepAgent15 小时前
AI Agent 面试篇(02):面试路线总览——每一关考什么、题什么样、要准备什么
面试·agent
颜挺锐16 小时前
如何轻松通过性能测试面试之第十一篇:如何判断应用的最大处理能力
面试·职场和发展
DeepAgent17 小时前
AI Agent 面试篇(01):AI 岗位面试到底怎么走——从网申到 Offer 的完整流程
人工智能·面试·职场和发展
林太白17 小时前
js-var和let以及const区别
前端·面试
智购科技自动售货机工厂19 小时前
2026自动售货机端侧AI降本逻辑:从云端API到本地推理的成本重构~YH
人工智能·python·ui·面试·交互
野生技术架构师19 小时前
1000+ Java面试题知识图谱:从基础语法到分布式架构的完整拓扑
java·面试
流浪00120 小时前
Linux系统篇34——信号(六):信号处理期间又来了怎么办?——sigaction、sa_mask、可重入函数与 volatile
linux·运维·面试·操作系统·信号处理·信号
ssshooter21 小时前
Node.js 天生就是异步的,为什么还要消息队列?
javascript·后端·面试