Recently Asked OAs in DE Shaw
Question 1: String Reversal
Click here to Practice
Submit Problem to OJ
Question 2
Click here to Practice
Submit Problem to OJ
Question 3: Minimum Cost Path:
Click here to Practice
Submit Problem to OJ
String Reversal
A series of numoperations are performed on a string initial_string of length n that contains lowercase English letters only. In the it operation, the prefix of the string of length / is reversed. The final string thus formed is represented by final string. Given the string final_ string and an integer num_operations, find the initial_string on which the operations were performed.
Example
Suppose final_string = "chakerrank" and num_operations = 3.
Then the initial string will be "hackerrank".
On the first operation, reverse "h" -> "hackerrank"
Reverse "ha" -> "ahckerrank"
Reverse "ahc" -> "chakerrank" which is equal to the final string.
Hence the answer is "hackerrank".
Function Description
Complete the function getinitialString in the editor below.
getinitialString takes the following arguments:
str final_string: The final string formed
int num operations: The number of operations performed
Returns
str. The initial string that results in the given string
Constraints
• 1≤ |final_string| ≤ 2 * 105
• 1 ≤ num_operations ≤ | final_string|
STD In Function
bbaaaba ---> final_string = "bbaaaaba"
Sample Output
aababba
Question 2
Given two strings, num, and num2, containing digits from 0 to 9 that represent
two integers. You are also given two integers, min sum, and max sum. Find the
number of integers x such that num1 ≤ x ≤ num2 and min_ sums ≤ sum of digits of
x ≤ max_sum. Since the answer can be large, report it modulo 109 + 7.
Example
Suppose min_ sum = 3, max, sum = 7, num 1 = 18 and num2 = 26.
The numbers in the range [18, 26] and their sum of digits are:
- 18, the digit sum is 9
- 19, the digit sum is 10
- 20, the digit sum is 2
- 21, the digit sum is 3
- 22, the digit sum is 4
- 23, the digit sum is 5
- 24, the digit sum is 6
- 25, the digit sum is 7
- 26, the digit sum is 8
STD In Function
24 ---> num1 = "24"
29 ---> num2 = "29"
2 ---> min_sum = 2
6 ---> max_sum = 6
Sample Output
1
3. Minimum Cost Path
A weighted directed graph can be represented with g. nodes nodes numbered from 0 to g_nodes • 1 and g_edges edges where the hedge connects the nodes,
numbered g_from[i) to g_toli] and has a weight g weight[o). The cost of a particular
path between two nodes is defined as the maximum weight of any edge that exists
in the path.
Given g_ nodes, g_edges, & from, g_to, and max_edges, find the minimum cost of a path from the node 0 to g_nodes - 1 that contains at most max_edges edges. If no
such path exists, report -1.
Example
Suppose &_ nodes = 4, g. edges = 5, max_edges = 2, g. from = [0, 1, 0, 2, 2], g. to = [1,3, 2, 3, 2], and g weight = (5, 6, 6, 5, 5].
The optimal path is from node 0 to 1 to 3 with the weights of the edges 5 and 6 respectively. Thus, the cost of the path is 6. Another path with cost 6 is 0 to 2 to 3.
Note that the path 0 to 1 to 2 to 3 has a cost 5 but can not be chosen as the number of edges in the path i.e. 3 is greater than max_edges = 2.
Function Description
Complete the function getMinCost in the editor below. getMinCost takes the following arguments:
int max_edges: the maximum number of edges to be taken in the path
int g_nodes: the number of nodes in the graph
ints. edges) & from: one end of the edges in the graph
inti[g_edges] g. to: the other end of the edges in the graph
int[g_edges] g weight: the weights of the edges in the graph
Returns
int: the minimum cost of the path from 0 to &_ nodes - 1 with at most max edges
edges.
constraints
•1 ≤ g_nodes ≤ 5*105
• 1 ≤ g_edges smin(2 * 105, g_nodes * (g nodes - 1))
• 1 ≤ g_from[i], & from(i] s g_nodes, g_from(i] != g. toli]
• 1 ≤ g_weightti] ≤ 109
• 1 ≤ max_ edges ≤ g_edges
It looks like the variation of the cheapest flight within K stop question.
why this code fails
#include <bits/stdc++.h>
using namespace std;
int CheapestFLight(int n, vector<vector<pair<ll,ll>>> &adj,
int src, int dst, int K)
{
queue<pair<int, pair<int, int>>> q;
q.push({0, {src, 0}});
vector<int> dist(n, 1e9);
dist[src] = 0;
while (!q.empty())
{
auto it = q.front();
q.pop();
int stops = it.first;
int node = it.second.first;
int cost = it.second.second;
if (stops > K)
continue;
for (auto iter : adj[node])
{
int adjNode = iter.first;
int edW = iter.second;
if (max(cost , edW) < dist[adjNode] && stops <= K)
{
dist[adjNode] = max(cost , edW);
q.push({stops + 1, {adjNode, max(cost , edW)}});
}
}
}
if (dist[dst] == 1e9)
return -1;
return dist[dst];
}
int main()
{
int n,m,k;
cin>>n>>m>>k;
vector<vector<pair<ll,ll>>> adj(n);
for(int i=0;i<m;i++){
ll a,b,c;
cin>>a>>b>>c;
adj[a].push_back({b,c});
}
cout<<CheapestFLight(n,adj,0,n-1,k);
}
I think we should apply bfs instead of dfs as we want minimum possible edges to reach (n-1) with weight <= mid.. As I infer dfs would fail on below testcase....Please correct me If I'm wrong.
5 5 2
0 1 5
1 2 6
2 4 6
0 3 5
3 4 5
you are right it can not be done by dfs normally........