0% found this document useful (0 votes)
24 views11 pages

Visteon Coding Patterns StepByStep

ggg

Uploaded by

dhrugenai12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views11 pages

Visteon Coding Patterns StepByStep

ggg

Uploaded by

dhrugenai12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Visteon Coding Patterns - Step by Step

10 high-yield coding patterns for MyAnatomy / HackerEart


with sample I/O, step-by-step reasoning, and clean Pytho

Use this as a last-minute revision kit.


# Pattern 1: Digit Confusion (5 6) Min & Max Sum

Problem:
Given two integers A and B. Due to dyslexia, digit '5' may be '6' and '6' may be '5'.
Print two values: (min_possible_sum, max_possible_sum).
Min: treat every 6 as 5. Max: treat every 5 as 6.

Input:
53 64
Output:
107 127

Step-by-step (for 53 64):


- Min (6 5):
A="53" -> "53" -> 53
B="64" -> "54" -> 54
Min sum = 53 + 54 = 107
- Max (5 6):
A="53" -> "63" -> 63
B="64" -> "64" -> 64
Max sum = 63 + 64 = 127

Python (input on one line):


```python
a, b = input().split()

amin = int(''.join('5' if ch == '6' else ch for ch in a))


bmin = int(''.join('5' if ch == '6' else ch for ch in b))

amax = int(''.join('6' if ch == '5' else ch for ch in a))


bmax = int(''.join('6' if ch == '5' else ch for ch in b))

print(amin + bmin, amax + bmax)


```
# Pattern 2: Normalize + Palindrome (ignore spaces/punct/case)

Problem:
Given a string S, check if it s a palindrome after removing non-alphanumeric chars
and ignoring case. Print "YES" or "NO".

Input:
A man, a plan, a canal: Panama
Output:
YES

Step-by-step:
- Normalize: keep only letters/digits, to lower-case.
- Compare normalized string with its reverse.

Python:
```python
import sys
import re

s = [Link]().strip()
norm = [Link](r'[^A-Za-z0-9]', '', s).lower()
print("YES" if norm == norm[::-1] else "NO")
```
# Pattern 3: Rotate Array Right by K

Problem:
Given N and K, then an array of N integers, rotate array to the right by K and print.

Input:
5 2
1 2 3 4 5
Output:
4 5 1 2 3

Step-by-step:
- Effective K = K % N
- Right rotate: last K elements come to front; rest follow.

Python:
```python
n, k = map(int, input().split())
arr = list(map(int, input().split()))
k %= n
rotated = arr[-k:] + arr[:-k] if k else arr
print(*rotated)
```
# Pattern 4: Count Subarrays with Sum = K (Prefix Sum + HashMap)

Problem:
Given N, K and array A, count number of subarrays that sum to K.

Input:
5 7
2 3 4 3 1
Output:
2

Step-by-step (prefix sums):


- Maintain running sum 'cur' and a dict 'freq' of prefix sums seen.
- For each value x:
cur += x
We need previous prefix 'cur - K' to form a subarray sum K.
Add freq[cur - K] to answer. Then increment freq[cur].

Python:
```python
from collections import defaultdict

n, k = map(int, input().split())
arr = list(map(int, input().split()))

freq = defaultdict(int)
freq[0] = 1
cur = 0
ans = 0

for x in arr:
cur += x
ans += freq[cur - k]
freq[cur] += 1

print(ans)
```
# Pattern 5: Anagram Check (two strings)

Problem:
Given two strings s and t, check if t is an anagram of s (ignore spaces/case). Print YES/NO.

Input:
Dormitory
Dirty room
Output:
YES

Step-by-step:
- Normalize both strings: keep letters only, same case.
- Compare frequency counts OR sorted strings.

Python (frequency map):


```python
import re
from collections import Counter

s = [Link](r'[^A-Za-z]', '', input()).lower()


t = [Link](r'[^A-Za-z]', '', input()).lower()

print("YES" if Counter(s) == Counter(t) else "NO")


```
# Pattern 6: Balanced Parentheses (Stack)

Problem:
Given a string with '()[]{}', determine if it's valid (properly nested).

Input:
{[()]}
Output:
YES

Step-by-step:
- Use stack.
- For each opening, push.
- For each closing, pop and check matching pair.

Python:
```python
pairs = {')':'(', ']':'[', '}':'{'}
s = input().strip()
stack = []
ok = True
for ch in s:
if ch in '([{':
[Link](ch)
else:
if not stack or stack[-1] != [Link](ch, '#'):
ok = False
break
[Link]()
if stack: ok = False
print("YES" if ok else "NO")
```
# Pattern 7: GCD and LCM of a List

Problem:
Read N and a list. Print gcd and lcm of all numbers.

Input:
4
2 4 8 16
Output:
2 16

Step-by-step:
- gcd(a,b) via [Link].
- lcm(a,b) = a*b // gcd(a,b).
- Reduce across the list.

Python:
```python
import math

n = int(input())
arr = list(map(int, input().split()))

g = arr[0]
l = arr[0]
for x in arr[1:]:
g = [Link](g, x)
l = l * x // [Link](l, x)

print(g, l)
```
# Pattern 8: DP Climbing Stairs (1 or 2 steps)

Problem:
Given n, number of distinct ways to reach the top if you can climb 1 or 2 steps at a time.

Input:
5
Output:
8

Step-by-step:
- dp[0]=1 (1 way to stand on ground), dp[1]=1.
- dp[i]=dp[i-1]+dp[i-2].

Python:
```python
n = int(input())
if n <= 1:
print(1)
else:
a, b = 1, 1 # dp[i-2], dp[i-1]
for _ in range(2, n+1):
a, b = b, a+b
print(b)
```
# Pattern 9: DP Minimum Path Sum in Grid

Problem:
Given rows, cols and the grid, move only right or down. Print min path sum from (0,0) to
(r-1,c-1).

Input:
3 3
1 3 1
1 5 1
4 2 1
Output:
7

Step-by-step:
- dp[i][j] = grid[i][j] + min(dp[i-1][j], dp[i][j-1])
- Initialize first row/col by cumulative sums.

Python:
```python
r, c = map(int, input().split())
grid = [list(map(int, input().split())) for _ in range(r)]

dp = [[0]*c for _ in range(r)]


dp[0][0] = grid[0][0]

for j in range(1, c):


dp[0][j] = dp[0][j-1] + grid[0][j]
for i in range(1, r):
dp[i][0] = dp[i-1][0] + grid[i][0]
for i in range(1, r):
for j in range(1, c):
dp[i][j] = grid[i][j] + min(dp[i-1][j], dp[i][j-1])

print(dp[-1][-1])
```
# Pattern 10: First Non-Repeating Character

Problem:
Given a string s, print the index (0-based) of the first non-repeating character. If none,
print -1.

Input:
leetcode
Output:
0

Step-by-step:
- Count frequency of each char.
- Scan string; return first index with freq=1.

Python:
```python
from collections import Counter

s = input().strip()
freq = Counter(s)
ans = -1
for i, ch in enumerate(s):
if freq[ch] == 1:
ans = i
break
print(ans)
```

You might also like