Sap Labs Interview Question for Senior Software Development Engineers


Country: India
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
0
of 0 vote

It seems like you have provided a coding problem related to counting k-Spikes in a given array of stock prices. You want to find the number of elements that satisfy specific conditions related to their neighboring elements. Here's a possible approach to solving this problem using Python:

```python
def count_k_spikes(prices, k):
n = len(prices)
count = 0

for i in range(n):
left_count = 0
right_count = 0

# Count elements to the left of prices[i] that are less than prices[i]
for j in range(i - 1, -1, -1):
if prices[j] < prices[i]:
left_count += 1
if left_count >= k:
break

# Count elements to the right of prices[i] that are less than prices[i]
for j in range(i + 1, n):
if prices[j] < prices[i]:
right_count += 1
if right_count >= k:
break

# Check if prices[i] is a k-Spike
if left_count >= k and right_count >= k:
count += 1

return count

# Example usage
prices = [1, 2, 8, 5, 3, 4]
k = 2
result = count_k_spikes(prices, k)
print("Output is:", result) # Output is: 2
```

This code defines a function `count_k_spikes` that takes an array of prices and the value of k as input. It then iterates through each element of the array, counting the number of elements to the left and right that are less than the current element. If both counts are greater than or equal to k, then the current element is considered a k-Spike, and the count is incremented. Finally, the function returns the total count of k-Spikes in the array.

- Abhishek August 31, 2023 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It seems like you have provided a coding problem related to counting k-Spikes in a given array of stock prices. You want to find the number of elements that satisfy specific conditions related to their neighboring elements. Here's a possible approach to solving this problem using Python:

```python
def count_k_spikes(prices, k):
n = len(prices)
count = 0

for i in range(n):
left_count = 0
right_count = 0

# Count elements to the left of prices[i] that are less than prices[i]
for j in range(i - 1, -1, -1):
if prices[j] < prices[i]:
left_count += 1
if left_count >= k:
break

# Count elements to the right of prices[i] that are less than prices[i]
for j in range(i + 1, n):
if prices[j] < prices[i]:
right_count += 1
if right_count >= k:
break

# Check if prices[i] is a k-Spike
if left_count >= k and right_count >= k:
count += 1

return count

# Example usage
prices = [1, 2, 8, 5, 3, 4]
k = 2
result = count_k_spikes(prices, k)
print("Output is:", result) # Output is: 2
```

This code defines a function `count_k_spikes` that takes an array of prices and the value of k as input. It then iterates through each element of the array, counting the number of elements to the left and right that are less than the current element. If both counts are greater than or equal to k, then the current element is considered a k-Spike, and the count is incremented. Finally, the function returns the total count of k-Spikes in the array.

- abhishekpeddi123 August 31, 2023 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More