Skip to content

Add solution for Challenge 7 by onomica#1884

Merged
github-actions[bot] merged 1 commit into
RezaSi:mainfrom
onomica:challenge-7-onomica
Jul 12, 2026
Merged

Add solution for Challenge 7 by onomica#1884
github-actions[bot] merged 1 commit into
RezaSi:mainfrom
onomica:challenge-7-onomica

Conversation

@onomica

@onomica onomica commented Jul 12, 2026

Copy link
Copy Markdown
Contributor

Challenge 7 Solution

Submitted by: @onomica
Challenge: Challenge 7

Description

This PR contains my solution for Challenge 7.

Changes

  • Added solution file to challenge-7/submissions/onomica/solution-template.go

Testing

  • Solution passes all test cases
  • Code follows Go best practices

Thank you for reviewing my submission! 🚀

@coderabbitai

coderabbitai Bot commented Jul 12, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

Adds a thread-safe Go BankAccount with typed validation errors, account construction checks, synchronized deposits and withdrawals, and deterministic locking for transfers.

Changes

Bank account operations

Layer / File(s) Summary
Account contracts and construction
challenge-7/submissions/onomica/solution-template.go
Defines typed errors, the transaction limit, the BankAccount structure, and constructor validation for account and balance values.
Single-account transactions
challenge-7/submissions/onomica/solution-template.go
Adds validated deposits and withdrawals protected by a mutex, including minimum-balance enforcement.
Synchronized transfers
challenge-7/submissions/onomica/solution-template.go
Transfers funds between accounts using deterministic lock ordering and source minimum-balance checks.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

Poem

A bunny deposits, then checks the floor,
Locks two accounts before hopping more.
No negative coins, no limits surpassed,
Transfers stay tidy, and deadlocks are passed.
Wiggle those balances—safe and fast!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main change: adding onomica's solution for Challenge 7.
Description check ✅ Passed The description is directly related to the Challenge 7 solution and its implementation details.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 golangci-lint (2.12.2)

level=error msg="[linters_context] typechecking error: pattern ./...: directory prefix . does not contain main module or its selected dependencies"


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2


ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 1c4c2429-a0f9-4f29-8a76-579631472695

📥 Commits

Reviewing files that changed from the base of the PR and between 1f00353 and e6f71e2.

📒 Files selected for processing (1)
  • challenge-7/submissions/onomica/solution-template.go

Comment on lines +114 to +144
func (a *BankAccount) Transfer(amount float64, target *BankAccount) error {
if a == target {
return nil
}

if err := validateAmount(amount); err != nil {
return err
}

first, second := a, target
if first.ID > second.ID {
first, second = second, first
}

first.mu.Lock()
defer first.mu.Unlock()
second.mu.Lock()
defer second.mu.Unlock()

if a.Balance-amount < a.MinBalance {
return &InsufficientFundsError{
Balance: a.Balance,
Amount: amount,
}
}

a.Balance -= amount
target.Balance += amount

return nil
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Search for Transfer-related tests in the challenge-7 test file
rg -n -C3 'TestTransfer' challenge-7/solution-template_test.go

Repository: RezaSi/go-interview-practice

Length of output: 302


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Show the Transfer test and nearby helpers with line numbers
sed -n '240,360p' challenge-7/solution-template_test.go | cat -n

Repository: RezaSi/go-interview-practice

Length of output: 4295


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Look for any other transfer-focused tests or self-transfer coverage
rg -n -C2 'self-transfer|Transfer\(|Concurrent.*Transfer|TestTransfer' challenge-7

Repository: RezaSi/go-interview-practice

Length of output: 17520


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Inspect the Transfer implementation and the test file around any concurrency helpers
sed -n '1,220p' challenge-7/submissions/onomica/solution-template.go | cat -n

Repository: RezaSi/go-interview-practice

Length of output: 4110


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Search for any dedicated self-transfer or concurrent transfer test cases
rg -n -C3 'self.?transfer|concurrent transfer|Transfer test|TestTransfer|go func\(\).*Transfer|\.Transfer\(' challenge-7/solution-template_test.go challenge-7

Repository: RezaSi/go-interview-practice

Length of output: 2771


Add self-transfer and concurrency cases to TestTransfer. The current test table covers invalid amounts and insufficient funds, but not the a == target early return or concurrent transfers that exercise the lock ordering.

Comment on lines +115 to +117
if a == target {
return nil
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Self-transfer skips amount validation.

The a == target early return at Line 115 happens before validateAmount at Line 119, so Transfer(-100, account) or Transfer(MaxTransactionAmount+1, account) returns nil instead of the expected NegativeAmountError/ExceedsLimitError. Move validation before the self-transfer check to keep the error contract consistent with Deposit and Withdraw.

🔧 Proposed fix
 func (a *BankAccount) Transfer(amount float64, target *BankAccount) error {
+	if err := validateAmount(amount); err != nil {
+		return err
+	}
+
 	if a == target {
 		return nil
 	}
 
-	if err := validateAmount(amount); err != nil {
-		return err
-	}
-
 	first, second := a, target
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if a == target {
return nil
}
func (a *BankAccount) Transfer(amount float64, target *BankAccount) error {
if err := validateAmount(amount); err != nil {
return err
}
if a == target {
return nil
}
first, second := a, target

@github-actions
github-actions Bot merged commit 84a0aa8 into RezaSi:main Jul 12, 2026
6 checks passed
@github-actions

Copy link
Copy Markdown

🎉 Auto-merged!

This PR was automatically merged after 2 days with all checks passing.

Thank you for your contribution, @onomica!

📊 Scoreboards and badges will be updated shortly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant