Share written test interview questions

Table of contents

1. Interval search

2. Broken number problem

3. Design a stack containing the main function to support the following operations. The algorithmic complexity of these operations is Changshu-level O(1)


1. Interval search

Given a sorted array num (with repeated elements), and the target value target, if the target appears in num, return the left and right subscripts [left subscript, right subscript] of the interval where the target is located, and return 0 if not

For example: num[]={5,7,7,8,8,8,8,10}, search for 8, then return [3,6];

Idea: What is given is a sorted array. In order to reduce the time complexity, we should choose to search by half . One search by half cannot find two values, so we divide it into two functions, and search by half.

int GetLeftIndex(int* num, int n,int k)
{
	int begin=0, end=n-1;
	int mid;
	while (begin <= end)
	{
		mid = (begin + end) / 2;
		if (num[mid] == k)
		{
			if (mid == 0 || num[mid - 1] < k)
				return mid;
			end = mid - 1;//mid之前的数如果等于k值,
		}
		else if (num[mid] > k)
		{
			end = mid - 1;
		}
		else if (num[mid] < k)
		{
			begin = mid + 1;
		}
	}
	return -1;//没找到的情况
}
int GetRightIndex(int* num, int n,int k)
{
	int begin = 0, end = n - 1;
	int mid = 0;
	while (begin <= end)
	{
		mid = (begin + end) / 2;
		if (num[mid] == k)
		{
			if (mid == 0 || num[mid + 1] >k)
				return mid;
			begin = mid + 1;
		}
		else if (num[mid] > k)
		{
			end = mid - 1;
		}
		else if (num[mid] < k)
		{
			begin = mid + 1;
		}
	}
	return -1;
}
void main()
{
	int num[] = { 5,7,7,8,8,8,8,10 };
	int n = sizeof(num) / sizeof(num[0]);
	cout << "[" << GetLeftIndex(num, n, 6) <<","<< GetRightIndex(num, n, 6) << "]" << endl;

}

screenshot:

 

2. Broken number problem

(Global scope) A secret-related unit has issued a certain kind of bill and wants to take back all of them at the end of the year. Each bill has a unique ID number. The ID numbers of all notes throughout the year are continuous, but the starting number of the ID is randomly selected. Due to the negligence of the staff, an error occurred when entering the ID number, which caused one ID number to be broken and another ID number to be repeated. Your task is to find out the ID of the broken number and the ID of the heavy number through programming. Assume broken numbers cannot occur between the largest and smallest numbers. The input format requires the program to first input an integer N (N <100) to indicate the number of subsequent data rows. Then read in N rows of data. The length of each line of data is different, and it is a number of (not greater than 100) positive integers (not greater than 100000) separated by spaces. Please note that there may be extra spaces in the line and at the end of the line. Your program needs to be able to handle these spaces. Each integer represents an ID number. The output format requires the program to output 1 line, containing two integers mn, separated by spaces. Among them, m represents the broken number ID, n represents the heavy number ID sample input 1

2

5 6 8 11 9 10 12 9

Sample output 1

7,9

Sample input 2 6

164 178 108 109 180 155 141 159 104 182 179 118 137 184 115 124 125 129 168 196 172 189 127 107 112 192 103 131 133 169 158 128 102 110 148 139 157 140 195 197 185 152 135 106 123 173 122 136 174 191 145 116 151 143 175 120 161 134 162 190 149 138 142 146 199 126 165 156 153 193 144 166 170 121 171 132 101 194 187 188 113 130 176 154 177 120 117 150 114 183 186 181 100 163 160 167 147 198 111 119

Sample output 2 105 120

Idea: Because the amount of data is not necessarily the same, we need to calculate the maximum and minimum values ​​of the data, so that there is a range when it is output. Secondly, define the number of occurrences of the marked data in the a array. After the traversal is completed, 0 represents the missing data, 1 represents the data that appears once, and 2 represents the data that appears repeatedly. Finally, you only need to output the data corresponding to 0 and 2.

code:

void main()
{
	int a[100001] = { 0 };
	int n, i;//n是行数,i是循环要用的
	int max = 0, min = 100000;
	char c;
	int value;
	scanf_s("%d", &n);
	for (i = 0; i < n; i++)
	{
		while (1)
		{
			scanf_s("%d", &value);
			if (value > max)
				max = value;
			if (value < min)
				min = value;
			a[value]++;//标志
			c = getchar();
			if (c != ' ')//c不是空格的时候,就是回车,可以进入下一行了
				break;
		}
	}
	for (i = min; i <= max; i++)
	{
		if (a[i] == 0)
			cout << "缺失的ID号是:" << i << endl;
		if (a[i] == 2)
			cout << "重复的ID号是:" << i << endl;
	}
}

screenshot:

3. Design a stack containing the main function to support the following operations. The algorithmic complexity of these operations is Changshu-level O(1)

push(x) pop() top() GetMin()---returns the smallest element in the stack

Idea: firstly, there should be no loops, and secondly, the use of stack, please check the code for details

#include<stack>
class MinStack
{
public:
	void Push(int x)//压栈
	{
		data.push(x);
		if (min.empty())//最小值的栈是空的
			min.push(x);
		else//栈不为空
		{
			if (x > min.top())//且大于此时的栈顶元素
			{
				x = min.top();//确保两个栈的元素是一样多的,如果数据栈的元素不入min栈的时候,把原来的栈顶元素又给了x,让原来min栈的最小值再次入栈,在pop的时候就不会出现错位,即pop数据栈里的元素,min栈有可能它的最小值还是原来的数值,但是一经pop出栈,那这个元素就没了,出现误差
			}
				min.push(x);//入栈
		}
	}
	void Pop()
	{
		data.pop();
		min.pop();
	}
	int Top()
	{
		return data.top();
	}
	int GetMin()
	{
		return min.top();
	}
private:
	stack<int> data;//存数据的栈
	stack<int> min;//存最小值的栈

};
void main()
{
	MinStack ms;
	ms.Push(-1);
	ms.Push(-5);
	ms.Push(3);
	ms.Push(4);
	ms.Push(7);
	cout << ms.GetMin() << endl;//-5
	ms.Pop();//出去7
	cout << ms.GetMin() << endl;//最小值还是-5,表明这时候min栈的-5不能出去,所以min栈存储了两份-5

}

 screenshot:

 

Guess you like

Origin blog.csdn.net/weixin_62456756/article/details/129115337