特斯拉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 A[i] and B[i] 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 A[P] + A[P+1] + ... + A[Q] 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]范围内的整数;

截图:

相关推荐
说给风听.3 分钟前
从零学会 Java 异常处理 —— 核心语法、自定义异常与面试指南
java·开发语言·面试
枳实-叶14 小时前
50 道嵌入式音视频面试题
面试·职场和发展·音视频
swipe14 小时前
向量数据库实战:为什么 AI Agent 离不开 Milvus
前端·面试·agent
认真的薛薛15 小时前
Docker网络模式
linux·运维·数据库·面试·github
深蓝轨迹17 小时前
TCP/IP 网络模型面试核心考点总结02(基础篇)
网络·tcp/ip·面试
青槿吖18 小时前
第二篇:Spring Boot进阶:整合异常处理、测试、多环境与日志,开发稳得一批!
java·spring boot·后端·spring·面试·sqlserver·状态模式
黄昏回响19 小时前
计算机系统基础知识(九):软件篇之网络协议详解
网络·网络协议·面试·改行学it
紫丁香19 小时前
高并发面试4
后端·面试·高并发
kyriewen21 小时前
for...of 的秘密:迭代器与可迭代对象,你也能创造“可循环”的东西
前端·javascript·面试
星辰_mya21 小时前
自定义注解 + AOP:打造企业级通用组件(日志、限流、幂等)
java·开发语言·spring·面试·架构师